Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking library without a header file?

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?

like image 773
eli Avatar asked Aug 07 '12 18:08

eli


People also ask

What happens if header file is not included?

When a header file is included two times in a C program, the second one gets ignored. In actual, the #, called the include, preceding a header file ensures that it is included only once during the compilation process.

Are header files necessary?

Yes, because it's still based on C. You can answer your own question: Don't use them and try to compile without them. If you can't, then the compilers still require them.

Should you include libraries in header files?

If your class declaration references types in the header, you will need to include it there. If it's only in the implementation, then you can include it in the cpp file.

Does static library need header files?

You can have the libraries installed without the headers. You can't then compile code that uses the header, but you can run code that uses the shared library.


1 Answers

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.

like image 106
Rafael Baptista Avatar answered Oct 14 '22 18:10

Rafael Baptista