0
votes

I am getting linking errors

error LNK2028: unresolved token (0A000018) "extern "C" void __clrcall MyCFunction(struct ud *)" (?MyCFunction@@$$J0YMXPAUud@@@Z) referenced in function "public: __clrcall MyCPPProj::MyClass::MyClass

error LNK2019: unresolved external symbol "extern "C" void __clrcall MyCFunction(struct ud *)" (?MyCFunction@@$$J0YMXPAUud@@@Z) referenced in function "public: __clrcall MyCPPProj::MyClass::MyClass

When I try to call functions in my C static library from a C++ class.

Here is how my library's header begins:

#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include "types.h"
extern void MyCFunction(struct myCStruct*);

Here is a snippet from the header's implementation:

extern void
MyCFunction(struct myCStruct* cStrInst)
{
    //Do stuff
}

and this is how I am calling it:

#include "MyCLib.h"
MyClass::MyClass() {
    myCStruct myCObj;
    MyCFunction(&myCObj);
    //More to follow...
}

Any one know where I am going wrong? All the advice I can find seems to have already been implemented, i.e. declaring whole header with extern "C" { wrapper (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html).

EDIT I've tried making the C library more C++ compliant and included it as C++ and C with the same result: LNK2028 and LNK2019 linker errors. I made it a static library because projects need to have a type and I needed to see it compile in isolation before adding it to my solution.

2
Why do you use extern if you implement the function in the .h file? What's the point of using a lib then? - crazyjul
I think __clrcall is your problem. No idea why it is there, though. - Patrick
The C code is not my own, I created a project from it but I think it was more from the GCC side of things. I want to mod it as little as possible so that I may incorporate updates from the developer more readily. Actually I've already made the source C++ compliant (added 33 explicit type casts and renamed the files *.CPP) so maybe I need to rename back *.C again.. - John
@John, where is the extern void MyCFunction(struct myCStruct* cStrInst)-implementation located. In a file that is compiled with C++ or C? - Patrick B.
@crazyjul The implementation is in another file, I would say the one corresponding to the header eg MyFile.h and MyFile.c but it is a little more tangled than that. - John

2 Answers

0
votes

For the implementation (in a .cpp-file) you need to put extern "C" like this:

extern "C" void MyCFunction(struct myCStruct* cStrInst)

to make the compiler produce the right symbol-name for a C-like-link.

0
votes

As a preliminary, though correct answer, I have found that adding a Form to the original native C library project (in its own solution - VS mutters a warning about converting the project which I just accepted), and ensuring all files are C++ (change suffix and explicitly casting) allows me to link to and call the function that was previously breaking. What's one more refactorisation?

So I'm happy with getting that first function to work. Onwards and upwards!

UPDATE Somewhere along the lines I was getting LNK2001 and LNK2019 errors again. This time relating to third party code. The fix was suprisingly easy (duplicate declaration replaced #pragma once #include) and prolly not picked up by the original compiler.

It all works great together now. Though I had an alternative LGPL / BSD solution if I couldn't use my original source :D