I\'m attempting to link a static library in C++ using Visual Studio 2010. Trouble is, the library (and accompanying header ) have a lot of MFC objects in them. I would like to c
You can absolutely do this, you just have to find the exact right function prototype.
Use "dumpbin" to dump the symbol table, and look for your function.
If the function name looks normal - then define it, and link to it using "extern C". If the symbol is c++ mangled, then you will need to demangle it to find the prototype.
If the function is not in the symbol table - then it has been defined statically in the lib, and is not accessible. Then you're hosed. There is no way.
For example if you have want ot call timeGetTime and you have a reason not to include mmsystem.h because of some conflicts, you can do this:
extern "C" DWORD WINAPI timeGetTime(void);
#pragma comment(lib, "winmm.lib")
A static library is the accumulation of one or more compiled modules. Each module can have dependencies on other modules, and some of those modules may be in other libraries.
If the function you require is in a module that has no other dependencies, or whose dependencies are all contained within the current library, you can link it once you have created a proper function prototype. If there are additional dependencies then you're out of luck.
If the function you want to call is using MFC bits you're going to have to added MFC support to your project as well. However, if it isn't, and the function has been exported by the library, you can simply add the prototype for the function in the file where you want to call it, and then link the library to your executable.
For instance, you'll add a line like this to the file where you're calling the exported function:
void __stdcall foo( int );
Make sure you get the calling convention correct, it may be different from __stdcall
. Also, you may have to add extern "C"
to prevent name mangling.