Does static library avoids name mangling issues?

旧街凉风 提交于 2021-02-19 08:34:20

问题


I have a C++\MFC application written in Visual Studio 2003 SP1 links to an external static library "SomeExtStaticLib.lib". I also include the header files provided with "SomeExtStaticLib.lib" to create the objects in my application.

SomeExtStaticLib.lib is a static library built with VC6.

Now, I am migrating my application to Visual Studio 2008.

I have very basic question.

Should I also migrate the "SomeExtStaticLib.lib" to VS2008 compiled one? When I tried to use this VC6 compiled "SomeExtStaticLib.lib" in my VC9 compiled application it did not give any linker errors. I was expecting at least some name mangling issues.

Are static libraries eliminating the name mangling issues?


回答1:


The issue isn't one of static vs. dynamic linking, nor really of name mangling. The issue is one of binary compatibility for everything used in the interface. Thus, for example, unless I'm badly mistaken, the definition of std::string changed between VC6 and VC9, with a different layout. So if any of the code uses std::string, you'll have to recompile, or get strange and unexplicable errors at runtime.

In general, it's best to assume no binary compatibility as soon as different versions of the compiler, or even different compilation options are involved, unless the vendor guarantees otherwise. (Although some common sense is in order: you can freely mix options which only control warnings, for example. But beware of /Ds which cause added debugging code to be generated or not.)




回答2:


If the application is unchanged it needs the same set of symbols from the library. And hence perhaps you can link to the library compiled with VC6.0. Name mangling is not the concern at all unless both the application and library are the same as the compatible(working) ones in VC6.0.

Should I also migrate the SomeExtStaticLib.lib to VS2008 compiled one?
There are compatibility problems between VC6.0 and visual 2008. So YES you should rebuild yor library with Visual 2008.

Just because you can link to the library as is doesn't mean it will work correctly.

Are static libraries eliminating the name mangling issues?
Not really. They are not doing anything special at all.




回答3:


Static libraries have nothing to do with or without name mangling.... if your code is C++ there is mangling, and if its C (or extern "C" in C++) then there is no mangling. As long as the library and the code that link it agree, there's no problem to link in the library.



来源:https://stackoverflow.com/questions/6342171/does-static-library-avoids-name-mangling-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!