Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

LNK4098 Linker Warning in Visual C++

📅 2010-Sep-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

Problem

C++ code when compiled can produce the linker warning LNK4098 at the final linking stage. This warning is of the form:

warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library

What does this mean?

It means that  more than one kind of C run-time library has been used in the project! This is the cause of the conflict. The different types of C run-time libraries can be seen here.

How to fix it?

 

One source of conflict is different source files being compiled with different options. Examine the Runtime Library properties of the each of the source files in the solution. Open Project > Properties and this option can be found at C/C++ > Code Generation > Runtime Library. There might be some files which differ in the Runtime Library they are using. For example, Foo.cpp might be compiled with Multi-threaded DLL (/MD) and Joe.cpp might be compiled with Multi-threaded (/MT). To fix this warning, change the Runtime Library settings so that they are uniform across all the files of the solution. Example: I typically notice these warnings when there are external Build Rules being used in the solution. For example, CUDA files (.cu) are typically compiled with a different Build Rule that invokes the CUDA compiler (nvcc.exe). The Runtime Library option within this Build Rule might not be matching the Runtime Library option of the solution, thus resulting in this warning.

Another possibility is the external libraries being linked with the project. Examine the library files being explicitly linked with the project and ensure they are compiled with the same run-time library options as that of the project.

If the above options fail or are not feasible, use the /NODEFAULTLIB option set to ignore the specific library that is causing this warning. That is, LIBCMTD in this case. To do this, go to Project > Properties > Linker > Input > Ignore Specific Default Libraries and add the filename of the offending library. For example, add LIBCMTD.lib in this case. This generates a compile option /NODEFAULTLIB:LIBCMTD.lib during compile time that excludes this library during linking.