简体   繁体   English

MSVC 2010链接器错误2005与静态库中的std :: cout

[英]MSVC 2010 linker error 2005 with std::cout in static library

I have found other examples of people having this problem but have had no luck with their solutions. 我找到了其他人遇到这个问题的例子,但他们的解决方案没有运气。 I am trying to use std::cout in a static library that also uses boost threads and bind. 我试图在一个也使用boost线程和绑定的静态库中使用std :: cout。 When I don't use and std::cout it compiles and links fine with the main program but when I do and I compile the library I have no problems but when I compile and link the main program that uses the static library I get a ton of things like: 当我不使用和std::cout它编译并与主程序良好链接但是当我这样做并且我编译库我没有问题但是当我编译并链接使用静态库的主程序时我得到了大量的事情:

2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj)
2>LIBCMT.lib(mlock.obj) : error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(mlock.obj) : error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR100D.dll)
2>LIBCMT.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)

etc... 等等...

I have tried going in to my linker settings and stopping it from using the conflicting library listed in the error output, but I can't get it right. 我已尝试进入我的链接器设置并停止使用错误输出中列出的冲突库,但我无法正确使用它。 If I tell it to stop using one library it may fix the problems but give me a few missing external symbols, then I switch the library it was having problems with but I still get a few "already defined". 如果我告诉它停止使用一个库它可以解决问题,但给我一些遗漏的外部符号,然后我切换它有问题的库但我仍然得到一些“已经定义”。 Any clues to what I can do to fix this? 有什么线索可以解决这个问题吗? Should I just make another class called "log" or something and just access it from the main .exe (which would use the strings library, which may cause problems again but I haven't tried it yet)? 我应该只创建另一个名为“log”或类别的类,只需从主.exe访问它(这将使用字符串库,这可能会再次引起问题,但我还没有尝试过)? Thank you for any help. 感谢您的任何帮助。

Try checking -> Project + Properties, C/C++, Code Generation, Runtime library. 尝试检查 - >项目+属性,C / C ++,代码生成,运行时库。 Your .lib and your main project must use the same setting here. 您的.lib和您的主项目必须在此处使用相同的设置。

Right-click your project, Properties, C/C++, Code Generation, Runtime Library setting. 右键单击项目,属性,C / C ++,代码生成,运行时库设置。 That's the source of your problem. 这是你问题的根源。 Settings there are /MT and /MD. 设置有/ MT和/ MD。 You are linking code that has conflicting values for this setting, everything must be compiled with the same one. 您正在链接具有此设置的冲突值的代码,所有内容必须使用相同的编译。

Boost could be the one. 提升可能是一个。 Check your linker's Additional Dependencies setting. 检查链接器的Additional Dependencies设置。 IIRC, the .libs have mt or md in their name. IIRC,.libs名字中有mt或md。

LNK2005 occurs when two compilation objects contain the same symbols. 当两个编译对象包含相同的符号时,会发生LNK2005。 When you try to link them together, the linker doesn't know what to do with the fact it has duplicates of everything, hence the error. 当您尝试将它们链接在一起时,链接器不知道如何处理它与所有内容重复的事实,因此错误。

Specifically, it sounds like your static lib / maybe boost may have been compiled with /MT which links the MSVC runtime in statically. 具体来说,听起来你的静态lib /也许是boost可能是用/MT编译的,它静态链接MSVC运行时。 It looks like you are trying to compile your code with the default options ( /MD , a dynamic link to MSVCR100.DLL , MSVCP100.DLL ). 看起来您正在尝试使用默认选项( /MDMSVCR100.DLLMSVCP100.DLL的动态链接)编译代码。

See http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx 请参阅http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx

You can either rebuilt said static library ensuring it is linked against the dynamic libraries, or try your code linked statically. 您可以重建所述静态库,确保它与动态库链接,或者尝试静态链接代码。

I should add that the D on the end just means debug. 我应该补充说,最后的D只是意味着调试。 ie MSVCP100D.DLL is the debug version of MSVCP100.DLL . MSVCP100D.DLL是调试版本MSVCP100.DLL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM