92

I am Integrating Matlab, C and Cuda together in a project. I used Matlab mix in order to connect matlab mx function written in c with the cuda runtime library, a linking error appear about conflict in static release and dynamic release between the c file and the library. Can anyone solve this?

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in file.obj. 
rwong
  • 6,062
  • 1
  • 23
  • 51
Ahmed Hassan
  • 961
  • 1
  • 6
  • 3

7 Answers7

91

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

Rahav
  • 1,755
  • 15
  • 18
  • 4
    This didn't fix the error for Severity Code Description Project File Line Suppression State Error LNK2038 mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in Calibration.obj OpenARK-SDK C:\OpenARK\OpenARK-SDK\libboost_system-vc140-mt-1_61.lib(error_code.obj) 1 – Mona Jalal May 08 '17 at 21:26
  • 3
    How to do this operation if I'm running my code via Console (CMD) ? – Scott Mar 25 '20 at 02:57
67

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.

tux3
  • 7,171
  • 6
  • 39
  • 51
  • 3
    What is the 'reason' for the requirement that all source files have been compiled with the preprocessor symbol _DEBUG defined? What inhibits to compile a compilation unit to be compiled for release for performance purposes? – harper Aug 18 '15 at 06:44
  • @harper: You **can** compile different compilation units with different preprocessor symbols defined. You **cannot** link against different versions of the CRT/C++ Standard Library, as explained in the link in this answer. The preprocessor symbols do not control, which CRT a library/application links against. This is controlled by command line switches passed to the linker. – IInspectable Feb 26 '16 at 11:29
  • 3
    How do you chage it in the UI? – xaxxon Apr 23 '16 at 07:44
  • 5
    @xaxxon Right click project -> Properties -> C/C++ -> Code Generation. The link option is listed in there. – EntangledLoops Aug 18 '16 at 18:20
  • @harper imagine headers or code bodies with #ifdef _DEBUG or equivalent in them. They are referenced by both Release and Debug builds. They also access member variables that are/are not defined based on _DEBUG, and may make other calls based on the build type. How would you get that to work? Reliably? – Technophile Nov 30 '20 at 22:22
  • @Technophile It's the responsibility of the developer to use code that make sense. This includes sensible conditional compilation. – harper Dec 01 '20 at 08:40
  • @harper The thing about "makes sense" is that it's in the eye of the beholder (here, the person saying "makes sense"). "Makes sense" will mean very different things based on the experiences the developers involved have had. Someone new to a topic may expect to "just work", while an experienced developer may know why should never be allowed because it breaks and , or allowed only in specific circumstances, or yes, that's a compiler limitation scheduled to be fixed in the next release. – Technophile Dec 07 '20 at 16:54
5

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.

GWKit
  • 79
  • 1
  • 3
5

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

user889030
  • 4,353
  • 3
  • 48
  • 51
  • 5
    The runtime library setting is completely independent of whether to build a .lib or a .dll. Do not change your library type to try to prevent a runtime library mismatch. – John Jul 28 '20 at 10:31
4

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.

hoopyfrood
  • 71
  • 2
  • Thanks. I experienced `'MT_StaticRelease' doesn't match value 'MTd_StaticDebug'`, even though everything was set to /MT. Changing the `Preprocessor Definition` from `_DEBUG` to `NDEBUG` solved my build problem. – Bart Aug 01 '23 at 09:59
0

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.

-1

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.