Visual Studio C++链接器警告:带有 C 运行时 (CRT) 的 LNK4006
我要问这个问题,然后自己回答。 我知道这是一个新手问题,但它花了我大约时间。这两天找到正确答案,无论如何我都会发布。
免责声明就这么多了——这是献给所有新手的:
我在 VS 2010 中进行了一个 MFC 项目,我需要添加一些第 3 方静态库。一切都很顺利,两个项目都已编译,并且我的项目成功引用了该库。然后,当我在项目中包含库中的一些头文件时,我收到了许多关于 C 运行时库中的函数的链接器警告,例如:
警告 9 警告 LNK4006: _sprintf 已在 libcmtd.lib 中定义( sprintf.obj);第二个定义忽略了 C:path\to\my\project\MSVCRTD.lib(MSVCR100D.dll)
想着“到底是什么”,我尝试运行我的项目,它确实运行了,直到遇到代码行它尝试使用“fostream”写入某个文件,然后由于一些可怕的堆损坏异常而崩溃。
搜索SO,我遇到了一些相关问题,但没有一个与我的问题完全匹配:
我几乎在我偶然发现答案之前,将外部库包装在 dll 中。
I'm going to ask this question, and then answer it myself.
I'm, aware that its a newbie question, but as it took me approx. two days to find the correct answer, I'll post it anyway.
So much for disclaimers - this one is dedicated to all you newbies out there:
I've go an MFC project in VS 2010, and I needed to add some 3rd party static library. All went well, both projects compiled and my project managed to reference the library. Then, when I included some header file from the library in my project, I got numerous a linker warnings, about functions in the C Run-Time library, e.g.:
Warning 9 warning LNK4006: _sprintf already defined in libcmtd.lib(sprintf.obj); second definition ignored C:path\to\my\project\MSVCRTD.lib(MSVCR100D.dll)
Thinking 'what the hell', I tried running my project, and it did run, until it came across a code line which tried to write into some file using 'fostream', and then it crashed with some scary heap corruption exception.
Searching S.O., I came across some related issues, none of which exactly match my problem:
- Link libraries with dependencies in Visual C++ without getting LNK4006
- How do you build a debug .exe (MSVCRTD.lib) against a release built lib (MSVCRT.lib)?
I almost wrapped the external library in a dll, before I stumbled across the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的幸福结局是我在 Microsoft 支持文章中偶然发现了正确的答案:如何链接到正确的 C 运行时 (CRT) 库。
显然,我违反了文章中所述的以下规则:
这意味着我应该为项目和第 3 方库使用相同的 C 运行时 (CRT) 库。在 VS 2010 中选择 CRT 可以通过以下方式完成:右键单击 project_name -->属性(属性窗口打开)--> C/C++ -->代码生成-->运行时库。使用下拉列表并根据文章中的表选择运行时库(单线程、静态多线程或dll多线程、发布或调试)。
配置完所有项目后,重新编译它们,(希望)警告消失了。
My happy ending is that I've stumbled across the correct answer in Microsoft support article: How to link with the correct C Run-Time (CRT) library .
Apparently, I violated the following rule, as stated in the articel:
Meaning that I should have used the same C Run-Time (CRT) library for both project and 3rd party library. Selecting CRT in VS 2010 can be done by: right-click project_name --> properties (the properties window opens) --> C/C++ --> Code Generation --> Runtime Library. Use the pull-down list and select the runtime library, according to the table in the article (single-threaded, static multi-threaded or dll multi-threaded, release or debug).
After configuring all projects, re-compile them, and (hopefully) the warnings are gone.