Visual Studio - 防止链接静态库

时间:2011-10-12 17:43:56

标签: visual-studio-2010 static-libraries

我正在Visual Studio 2010中创建一个静态库。这个库将与我的另一个应用程序静态链接以生成.exe。问题是我希望我的.exe与C adn C ++库静态链接(不希望依赖于msvcp100.dll和msvcr100.dll)。但是,无论我做什么,我都无法让它发挥作用。

如果我将静态库与静态C和C ++库链接,那么我无法编译.exe - 链接器抱怨“已经定义的符号”。

如果我将我的静态库与C和C ++ DLL链接,那么我的.exe最终依赖于msvcp100.dll和msvcr100.dll。

如果我的静态C和C ++库在我的.exe中被链接,我如何告诉VS链接我的静态库?

修改

当静态lib和.exe用户/ MT(即运行时库的静态链接)时,以下是fre链接器错误:

1>msvcrt.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref@facet@locale@std@@QAEPAV123@XZ) already defined in re2_release.lib(regexp.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "protected: virtual class std::basic_streambuf<char,struct std::char_traits<char> > * __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::setbuf(char *,__int64)" (?setbuf@?$basic_streambuf@DU?$char_traits@D@std@@@std@@MAEPAV12@PAD_J@Z) already defined in re2_release.lib(regexp.obj)
1>msvcrt.lib(MSVCR100.dll) : error LNK2005: _strtoul already defined in LIBCMT.lib(strtol.obj)
1>LIBCMT.lib(crt0init.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

如果我设置/ NODEFAULTLIB:LIBCMT,则mscvrt.lib中的错误消失,但msvcprt.lib错误仍然存​​在。

1 个答案:

答案 0 :(得分:3)

我有这个问题,libriks解决方案解决了它。我会在这里引用他的那些跳过阅读评论的人:

  

某些模块未使用运行时库的静态链接,因为链接器错误&gt;显示“msvcrt.lib”,它是DLL版本的导入库。仔细查看&gt;通过您的项目设置以及您链接到的任何其他库,确保一切都是&gt;使用/ MT。

相关问题