Visual Studio C++链接器警告:带有 C 运行时 (CRT) 的 LNK4006

发布于 2024-10-03 13:36:51 字数 907 浏览 3 评论 0原文

我要问这个问题,然后自己回答。 我知道这是一个新手问题,但它花了我大约时间。这两天找到正确答案,无论如何我都会发布。

免责声明就这么多了——这是献给所有新手的:

我在 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:

I almost wrapped the external library in a dll, before I stumbled across the answer.

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

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

发布评论

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

评论(1

傲鸠 2024-10-10 13:36:51

我的幸福结局是我在 Microsoft 支持文章中偶然发现了正确的答案:如何链接到正确的 C 运行时 (CRT) 库

显然,我违反了文章中所述的以下规则:

可重用库及其所有用户应使用相同的 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:

A reusable library and all of its users should use the same CRT library types and therefore the same compiler switch.

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.

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