简体   繁体   中英

Is it possible to link a debug mode compiled library to a project in release mode?

I have a compiled library (static .lib) in debug mode in C++ and Visual Studio and want to use it in a project which is going to be compiled in release mode. Is this possible? I couldn't compile the library in the release mode :(
Let me ask a little bit more general question: what considerations should I care about working with libraries?

With Visual Studio (2005 and later) classes such as STL containers and iterators will by default be defined differently, and have different memory layout, in debug and release mode. If you mix the debug and release versions of such classes by linking with a static library (or calling a dynamic library whose interface contains such classes) then you will run into trouble.

I believe, but I'm not 100% sure, that with MSVS 2005 and 2008 the code will link, and you will get a mysterious crash at run time, and with MSVS 20010 and 2012 the code will fail to link.

When compiling in debug mode, you can force Visual Studio to use the release versions of STL containers and iterators by defining the macro _SECURE_SCL as 0.

Is it possible to link a debug mode complied library to a project in release mode?

Yes. Debug mode only means there are assertions, debug symbols included, etc. The debug version of the library should be fully functional just like the release version (although it might run slower/consume more memory than a release build).

Edit: as David Rodriguez-dribeas points out, this can yield undefined behavior if a definition of an inline function is different in release and debug mode, as it then violates the one definition rule.

what considerations should I care about working with libraries?

That's too broad to be answered on StackOverflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM