1

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
}
JoshD
  • 21
  • 3
  • #πάνταῥεῖ - Exactly the solution in the "dup" isn't working. This is not a dup (or the dup). – rustyx Feb 20 '17 at 15:23
  • Can you please read the OP in full and not mark duplicate? I understand this sounds like a generic question with a simple answer but I have incorporated this into my question to ask something different. – JoshD Feb 20 '17 at 15:25
  • 1. Check that the `NrcsDriver_HSUSRT_If.o` contains the symbol `Hsusrt_getTxBuffer` (and not a name-mangled version). 2. Check that the `NrcsDriver_HSUSRT_If.o` object file is added at the link stage. – Klas Lindbäck Feb 20 '17 at 15:25
  • It is likely that you have compiled the C source as a C++ one, because the linker cannot find the unmangled symbol. It is not a problem of C/C++ linkage but a build problem. Please describe how you build the executable. – Serge Ballesta Feb 20 '17 at 15:32
  • It's very close to the dupe. I'd be _almost_ tempted to re-open this if you had a useful MCVE. – Lightness Races in Orbit Feb 20 '17 at 15:32
  • @KlasLindbäck can you point to any resource on how to do this? – JoshD Feb 20 '17 at 15:35
  • @JoshD: `NrcsDriver_HSUSRT_If.c` will not compile as C unless you somehow include `stdbool.h`. If you compile a file as C++, it is not C. Identical syntax does not imply identical semantics! If this was re-opened without more information, I'd close-vote for missing [mcve] and other information. Read [ask]. – too honest for this site Feb 20 '17 at 15:42
  • @Olaf Thank you for the comment. I irresponsibly chose bool as an arbitrary return type. Changing it void however yields the same result. – JoshD Feb 20 '17 at 15:50
  • @JoshD: how aout reading my comment **carefully** again. From your last comment, you did not understand its implications. – too honest for this site Feb 20 '17 at 16:12
  • @Olaf I read your comment, the link provided, and edited the post and admitted my mistake. What is not obvious to me is what information can be added to make it more "MCV" – JoshD Feb 20 '17 at 16:29
  • @JoshD you missed the point. You still might compile as C++, not C. Another issue if that woul dbe C is not using a correct prototype declaration for your function. Something like `int f();` has different semantics in C and C++. In C you should use a prototype `int f(void);` – too honest for this site Feb 20 '17 at 16:33
  • @JoshD What Olaf is saying is that it looks like your IDE is compiling `NrcsDriver_HSUSRT_If.c` as `C++`, and since the function definition isn't surrounded by `extern C { }` it is name-mangled. A solution would be to tell the compiler to compile all files with the extension `.c` as C files. – Klas Lindbäck Feb 21 '17 at 08:28

0 Answers0