开发者

Visual Studio C++ linker warning: LNK4006 with C Run-Time (CRT)

开发者 https://www.devze.com 2023-01-26 17:34 出处:网络
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.

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)

Thi开发者_高级运维nking '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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消