12

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 call one of the library's functions without recompiling my project to include MFC, or recompiling the library to not use MFC. This codeproject article seems to imply that this is possible if I define the function to be external in my project (using the "extern" keyword).

However, I've had no luck. No matter what I try, I get an unresolved external symbol error.

Is the article correct? And if not, is such linkage possible any other way?

eli
  • 311
  • 1
  • 2
  • 10
  • Functions have external linkeage by default, so `extern` is redundant. Are you sure the functions are exported from the lib? Are you sure they have the calling convention you expect (i.e. not `extern C`)? – Luchian Grigore Aug 07 '12 at 18:58
  • @eli Its a long article you have linked to. Which technique are you referring to? – quamrana Aug 07 '12 at 19:08
  • @quamrana Its about a third of the way down, the second paragraph under "The drag and drop method." Linking without a header is mentioned only briefly. – eli Aug 07 '12 at 19:17
  • @eli I see. Basically you need a header file *and* link with the supplied lib. If you don't have an official header file you can write your own, guessing the function signature. – quamrana Aug 07 '12 at 19:23

4 Answers4

13

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.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
  • Very precise answer. Unless the needed function prototypes include explicitly MFC objects (such as `CString`). – valdo Aug 07 '12 at 19:17
  • 1
    Solved the issue I asked about, unfortunately it doesn't look like I will be able to bring in the library without bringing in the MFC libraries into my current project. Appreciate the help. – eli Aug 08 '12 at 15:24
0

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.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • This was the answer I was hoping I wouldn't hear. I thought the beauty of statically linked libraries were that they were "complete" collections of code, with no need to add other libraries as dependencies ( such as MFC ). – eli Aug 07 '12 at 19:12
  • @eli unfortunately it's quite common for libraries to depend on other libraries, whether static or dynamic. – Mark Ransom Aug 07 '12 at 19:17
0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

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")
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148