Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 976
Szymon Wybranski Avatar asked Aug 23 '10 05:08

Szymon Wybranski


People also ask

Is .LIB a static library?

Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, . a files in Linux and . lib files in Windows.

Is a .LIB file compiled?

LIB is a static library where functions and procedures can be placed and called as the application is being compiled.

How do I create a .LIB file in CPP?

To create a static library project in Visual StudioOn the menu bar, choose File > New > Project to open the Create a New Project dialog. At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library.


1 Answers

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.

like image 137
Chris Becke Avatar answered Oct 07 '22 16:10

Chris Becke