views:

69

answers:

4

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?

+1  A: 

One idea would be not to use any CRT functions.

Pavel Radzivilovsky
+1  A: 

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 then 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.

Chris Becke
A: 

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.

David Feurle
DLL's with different runtimes ca _not_ be easily mixed. Any attempt to pass a `std::string` across DLL boundaries requires both sides to have the same implementation - and in practice, that doesn't even hold across compiler versions.
MSalters
If you pass classes/instances of STL types as arguments from/to the DLL it depends on the runtime. If you don't do this the DLL won't be dependant. I would suggest to not export classes but just plain C from a DLL. A Lib would depend on the runtime even if the STL types are not used in the interface.
David Feurle
A: 

Do the two Projects use the same value for treat wchar_t as builtin type?

David Feurle