Is it possible to create a static library (a single .lib file) that can be compiled with / MT, / MTd, / MD or / MDd?

Instead of creating 4 different libraries (one for MT, MTd, MD, MDd ) I want to create a lib that does not determine its dependency on the runtime library (CRT).

I tried passing the "/ c / Zl" parameter to the vc10 compiler, then / NODEFAULTLIB to the lib command. Later, when I use such a lib, I still have errors when I compile my program using a switch other than default / MT. for example / MD here are the first few errors:

msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct st d::char_traits<char> >::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > & (__cdecl*)(class std::basic_ostream<char,struct std::char_tra its<char> > &))" ( ??6?$basic_ostream@DU ?$char_traits@D @ std@ @@ std@ @ QAEAAV01@P6AAAV01 @ AAV01@ @ Z@Z ) already defined in lib.lib(lib.obj) msvcprt.lib(MSVCP100.dll) : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::endl(class std::basic_ostream<char,stru ct std::char_traits<char> > &)" ( ?endl@std @@ YAAAV?$basic_ostream@DU ?$char_traits@D @ std@ @@ 1@AAV21 @@Z) already defined in lib.lib(lib.obj) 

Is it possible to create a static library (single .lib file), which can later be compiled in the final programs using / MT, / MTd, / MD or / MDd?

+6
c ++ visual-c ++ command-line-arguments compiler-errors static-libraries
source share
3 answers

I would say that / MT / Zl are important parameters needed to create a "neutral" lib file.

The problem is that in C ++ there is some conflict, not c runtime. He seems to have decided to add implementations of some template classes to the lib.lib file - and you can understand why - in the / MT assembly you told the compiler that dll c-runtime cannot use pre-compiled forms of common template instances - so the STL header files will choose to be embedded.

Perhaps there are some additional macro definitions that determine how STL header files are chosen to reveal their functionality. Not knowing what it is, it seems that this is a simple rule: you cannot actually create a neutral version of runtime if you are using STL.

+2
source share

One idea would be to not use any CRT features.

+1
source share

You can use DLL instead of Lib. DLL forms a separate communication domain. DLLs with different versions / compilers can be easily mixed.

0
source share

All Articles