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.
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 😀