I am trying to call a C function from inside a C++ project in Visual Studio 2015. The error I get is LNK2019 "unresolved external symbols"
Error LNK2019 unresolved external symbol _Hsusrt_getTxBuffer referenced in function "private: bool __thiscall hsbuf::TxHsusrtBuffer::getBuffer(void)" (?getBuffer@TxHsusrtBuffer@hsbuf@@AAE_NXZ) inter_device_d3_r5 C:\[path]\build\test.obj
I have a .h file with the function declaration and .c file with the definition below.
NrcsDriver_HSUSRT_If.h:
//func prototype
void Hsusrt_getTxBuffer( ) ;
NrcsDriver_HSUSRT_If.c:
#include "NrcsDriver_HSUSRT_If.h"
void Hsusrt_getTxBuffer( )
{
//dummy func implementation here
}
I can get it to work two ways. One is pre and post appending the C++ guards in the C header file
//#ifdef __cplusplus
//extern "C" {
//#endif
//func prototype
void Hsusrt_getTxBuffer( ) ;
//#ifdef __cplusplus
//extern }
//#endif
Second, I can do a standard #include in a test.cpp without any special extern "C" guards
**test.cpp**
#include "NrcsDriver_HSUSRT_If.h"
int main()
{
Hsusrt_getTxBuffer(); //works fine
}
However, I am very confused why it doesn't work when I wrap the #include in the extern "C" inside the .cpp file. Everything I have read dictates this to be the correct and proper way to include a C header. Yet this is technique fails for my project.
**test.cpp**
extern "C" {
#include "NrcsDriver_HSUSRT_If.h"
}
int main()
{
Hsusrt_getTxBuffer(); //does not work
}