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

发布于 2024-10-10 17:07:48 字数 1114 浏览 7 评论 0原文

我发现了其他遇到此问题的人的例子,但没有找到解决方案。我正在尝试在静态库中使用 std::cout,该库也使用 boost 线程和绑定。当我不使用和 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)

等等...

我尝试进入我的链接器设置并阻止它使用错误输出中列出的冲突库,但我无法正确处理。如果我告诉它停止使用一个库,它可能会解决问题,但会给我一些丢失的外部符号,然后我切换它有问题的库,但我仍然得到一些“已定义”。有什么线索可以告诉我我可以做些什么来解决这个问题吗?我是否应该创建另一个名为“log”的类或其他类,然后从主 .exe 访问它(它将使用字符串库,这可能会再次导致问题,但我还没有尝试过)?感谢您的帮助。

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

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)? Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-10-17 17:07:48

尝试检查 ->项目 + 属性、C/C++、代码生成、运行时库。
您的 .lib 和主项目必须在此处使用相同的设置。

Try checking -> Project + Properties, C/C++, Code Generation, Runtime library.
Your .lib and your main project must use the same setting here.

半世蒼涼 2024-10-17 17:07:48

右键单击您的项目,属性,C/C++,代码生成,运行时库设置。这就是你的问题的根源。设置有/MT 和/MD。您正在链接的代码与此设置的值有冲突,所有内容都必须使用相同的值进行编译。

Boost可能就是其中之一。检查链接器的附加依赖项设置。 IIRC,.lib 的名称中包含 mt 或 md。

Right-click your project, Properties, C/C++, Code Generation, Runtime Library setting. That's the source of your problem. Settings there are /MT and /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. IIRC, the .libs have mt or md in their name.

深海蓝天 2024-10-17 17:07:48

当两个编译对象包含相同的符号时,就会发生 LNK2005。当您尝试将它们链接在一起时,链接器不知道如何处理它具有所有内容的重复项,因此会出现错误。

具体来说,听起来你的静态库/也许 boost 可能是用 /MT 编译的,它静态链接 MSVC 运行时。看起来您正尝试使用默认选项(/MDMSVCR100.DLLMSVCP100.DLL 的动态链接)编译代码)。

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

您可以重建所述静态库,确保它与动态库链接,或者尝试静态链接您的代码。

我应该补充一点,末尾的 D 仅意味着调试。即MSVCP100D.DLLMSVCP100.DLL的调试版本。

LNK2005 occurs when two compilation objects contain the same symbols. 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. It looks like you are trying to compile your code with the default options (/MD, a dynamic link to MSVCR100.DLL, MSVCP100.DLL).

See 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. i.e. MSVCP100D.DLL is the debug version of MSVCP100.DLL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文