LNK2038: 检测到‘ RuntimeLibrary’的不匹配: 值‘ MT_Staticrelease’与 file.obj 中的值‘ MD_Dynamicrelease’不匹配

我整合 MatlabCCuda一起在一个项目。为了将用 c 语言编写的 Matlab mx 函数与 cuda 运行时库连接起来,在 c 文件和库之间出现了静态释放冲突和动态释放冲突的链接错误。有人能解决这个问题吗?

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in file.obj.
122365 次浏览

The library and your project must be linked with the same settings with regards to the C Runtime Library.

In your case one was linked against the CRT DLL (/MD) and the other was linked statically (/MT).

You just need to make sure both match and this error will go away.

This error can occur when you are statically linking your project with a library (typically a file with .lib extension) but the linker setting in your Visual Studio project are set to dynamically link (meaning the link will occur during runtime, usually with a .dll file).

To define that you need the project to use static linking start Visual Studio. In the Solution Explorer pane, right click the project name, and select Properties. Expand the properties as shown in the figure below: C/C++ --> Code Generation --> Runtime Library, select the Multi-threaded (/MT) option from the dropdown menu. enter image description here

for sharing purpose.

I'm using 2017 VS version which successfully open and run an old 2008 solution. Now, if for some reason, even if you change all your libraries and your main project to have the same runtime library param (under properties, see above posters) but you are still getting the same error message, try opening each individual .vcxproj file. Search under "RuntimeLibrary" and make their value same throughout all the vcxproj files. For some reason, these vcxproj files never update to the same value that I stated in the properties settings and I have to change them manually in the vcxproj.

Optionally, if you wish, open vcproj files too and change their "RuntimeLibrary" to be the same as well. Here the value is in digit.

This would work better as a comment to GWKit but I don't have the reputation for it. He mentions having to update the vcxproj files because they don't actually change. In my case they only saved after hitting "saveAll" and then closing visual studio. I got a prompt asking if i want to save changes to properties which were saved and after clicking yes the vcxproj files were properly updated.

As others have mentioned the runtime library switch on cl.exe must match between all of the compiled modules. In MSBuild this is referenced as ClCompile>/RuntimeLibrary.

However, even if these match you might still encounter this problem if there is a "#undef DEBUG" or "#undef _DEBUG" somewhere in your project. The yvals.h header that is part of the VC++ runtime library headers can change what is compiled into your obj files if these macros are changed.

Use "dumpbin /all foo.obj >foo.txt" to check what is actually going into your obj files. Look for the header "Linker Directives" in that output.

Here are proper steps to fix

Error   69  error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease'

this mean that runtime lib is static aka lib ( MT_StaticRelease ) Which is different then you project value Dynamic Lib aka dll (MD_DynamicRelease)

  • right click on solution
  • click properties
  • configuration properties -> General

change Configuration Type to Static Lib (lib) from Dynamic Lib (dll)

enter image description here

In case you have reverse scenario and the above steps not fix problem then play with this option

C/C++ --> Code Generation --> Runtime Library select Multi threaded enter image description here

This linker error occurred due to the improper project configuration, may be you have built the library in a configuration which is different from the main project configuration . If your project configuration is release\debug, then you should choose the same configuration while building your library.