Szymon Wybranski
Szymon Wybranski

Reputation: 646

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

Instead of creating 4 different libs (one for MT, MTd, MD, MDd) I want to create a lib that does not specify its dependency on C runtime library (CRTs).

I tried to pass "/c /Zl" option to vc10 compiler, then /NODEFAULTLIB to lib command. Later when I use such lib I still have errors when I compile my program with switch different than default /MT. e.g. /MD here are few first 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) that can be later compiled in final programs with either /MT, /MTd, /MD or /MDd?

Upvotes: 6

Views: 1506

Answers (3)

Chris Becke
Chris Becke

Reputation: 36026

I would normally have said that /MT /Zl are the important options to have to make a 'neutral' lib file.

The problem here is that there is some kind of conflict in the c++ rather than c runtime. It seems to have decided to add the realizations of some template classes to the lib.lib file - and one can kind of understand why - in a /MT build you've told the compiler that the c-runtime dlls can't use the precompiled forms of the common template instantiations - so the STL header files will choose the variant that gets built in.

There are possibly some additional macro definitions that control how the STL header files choose to expose their functionality. Without knowing what they are it seems that the simple rule is: You can't actually make a runtime neutral lib if the STL is being used.

Upvotes: 3

David Feurle
David Feurle

Reputation: 2787

You could use a DLL instead of a Lib. A DLL forms a seperate link domain. DLL's with different runtimes/compilers can easily be mixed.

Upvotes: 0

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19114

One idea would be not to use any CRT functions.

Upvotes: 1

Related Questions