C++ library linking to C++ dll using g++
I've seen numerous questions and answers to people with C libraries linking to C++, or C++ libraries linking to C... I however have a C++ library linking to C++, and am getting the same symptoms as people mixing C and C++ together (undefined reference during linking).
This is my g++ line:
g++ -L C:/MyLibraries mycode.cpp -shared -o mycode.dll -lopengl32 -lglu32 -lgdi32 -lMyLibrary
Every answer I've come across so far talks about having the following wrapping my header:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
However, my library is pure C++, 100% classes, no global functions, nothing that an extern "C" can penitrate (can't extern "C" on the methods in the classes, and wrapping the entire set of classes with extern "C" does nothing... still undefined references when linking).
MyLibrary.lib was writen in Visual Studio 2010. And, I have written many libs using Visual Studio 2005 and 2008. Not a single one ever needed extern "C" added to any of them to statically link them into other Visual Studio based projects. C++ libraries linked into other C++ project, with no problems, ever.
Is this something that Visual Studio takes care of that g++ does not? I can staticly link a large number of my own C++ custom static libs together into other C++ projects, and it just works. Is Visual Studio just smart enough to decipher it's own C++ method name mangling, so I've never run into this until now? But g++ doesn't know what to do, forcing C naming conventions to be required (even though I'm 100% C++ on all ends)?
Or is the problem that even though I'm using g++, its still enforcing some standard C rules? Something wrong with my g++ command line?
Even looking for an answer at MSDN for exporting dll functions makes it look more like I've just got something wrong with my g++ line, because their links have subjects such as "开发者_运维技巧Export C functions into C++ executables" and "Export C++ functions into C executables", with no link for "Export C++ functions into C++ executables" mentioned anywhere... I tried __declspec(dllexport) on my functions, which compiled, but still undefined linking with g++ still. Any ideas?
Thank you.
Visual Studio and g++ use very different conventions for things like name mangling, vtable layout, and such so that it is not safe to link libraries built with them together (other than when they only communicate through C interfaces). There is more information at http://www.mingw.org/wiki/MixingCompilers on the MinGW site.
精彩评论