I am developing an application on Microsoft Visual C++ (MSVC) that uses DCMTK. When I try to compile and link my application against the DCMTK libraries, the linker fails with error LNK2005 and reports warning LNK4098, e.g.
LIBCMTD.lib(dbgheap.obj) : error LNK2005: _malloc already defined in MSVCRTD.lib(MSVCRTD.dll)
LINK : warning LNK4098: defaultlib "LIBCMTD" conflicts with use of other libs; use /NODEFAULTLIB:library
The problem is that the linker tries to combine different, incompatible versions of the Visual C++ runtime library into a single binary. This happens when not all parts of your project and
the libraries you link against are generated with the same code generation options in Visual C++. Do not
use the /NODEFAULTLIB workaround, because strange software crashes may follow. Fix the problem!
DCMTK is by default compiled with the "Multithreaded" or "Multithreaded Debug" code generation option (the latter for Debug mode). Either change the project settings of all
of your code to use these code generation options, or change the code generation for all
DCMTK modules and re-compile. MFC users beware: DCMTK should be compiled with "Multithreaded DLL" or "Multithreaded DLL Debug" settings if you want to link the libraries into an MFC application.
Note: You can change the code generation options centrally in file dcmtk/CMakeLists.txt by replacing "/MT" with "/MD" and "/MTd" with "/MDd". You then have to run CMake to create new project files for the complete DCMTK toolkit. If you do this change, however, please note that you also have to provide all external libraries (zlib, openssl etc.) in pre-compiled versions generated with "/MD" or "/MDd". Alternatively, you can disable the use of this external libraries in CMake.