Wednesday, December 16, 2020

Warning LNK4098 defaultlib 'MSVCRT' conflicts with use of other libs;

Warning LNK4098 defaultlib 'MSVCRT' conflicts with use of other libs;

  1. LINK : warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs
  2. Resolving LNK4098: defaultlib 'MSVCRT' conflicts with
  3. this resolved my issue:

    get this every time I want to create an application in VC++:

    1. Right-click the project, select Properties then under 'Configuration properties | C/C++ | Code Generation', select "Multi-threaded Debug (/MTd)" for Debug configuration.
    2. Note that this does not change the setting for your Release configuration - you'll need to go to the same location and select "Multi-threaded (/MT)" for Release.

  4. Resolving LNK4098: defaultlib 'MSVCRT' conflicts with
  5. There are 4 versions of the CRT link libraries present in vc\lib:

    • libcmt.lib: static CRT link library for a release build (/MT)
    • libcmtd.lib: static CRT link library for a debug build (/MTd)
    • msvcrt.lib: import library for the release DLL version of the CRT (/MD)
    • msvcrtd.lib: import library for the debug DLL version of the CRT (/MDd)

    Look at the linker options, Project -> Configuration Properties-> Linker -> Command Line. Note how these libraries are not mentioned here. The linker automatically figures out what /M switch was used by the compiler and which .lib should be linked through a #pragma comment directive. Kinda important, you'd get horrible link errors and hard to diagnose runtime errors if there was a mismatch between the /M option and the .lib you link with.

    You'll see the error message you quoted when the linker is told both to link to msvcrt.lib and libcmt.lib. Which will happen if you link code that was compiled with /MT with code that was linked with /MD. There can be only one version of the CRT.

    /NODEFAULTLIB tells the linker to ignore the #pragma comment directive that was generated from the /MT compiled code. This might work, although a slew of other linker errors is not uncommon. Things like errno, which is a extern int in the static CRT version but macro-ed to a function in the DLL version. Many others like that.

    Well, fix this problem the Right Way, find the .obj or .lib file that you are linking that was compiled with the wrong /M option. If you have no clue then you could find it by grepping the .obj/.lib files for "/MT"

    Btw: the Windows executables (like version.dll) have their own CRT version to get their job done. It is located in c:\windows\system32, you cannot reliably use it for your own programs, its CRT headers are not available anywhere. The CRT DLL used by your program has a different name (like msvcrt90.dll).

    An trick I just learned to track down libraries that are pulling in the wrong CRT libraries is to add /verbose:lib to the additional linker options. It shows the order that .lib files are loaded in, allowing you to see where the incorrect one was pulled in. – obmarg Mar 13 '13 at 12:18

  6. Linker Tools Warning LNK4098
  7. defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:library

    You are trying to link with incompatible libraries.

    Important The run-time libraries now contain directives to prevent mixing different types. You’ll receive this warning if you try to use different types or debug and non-debug versions of the run-time library in the same program. For example, if you compiled one file to use one kind of run-time library and another file to use another kind (for example, single-threaded versus multithreaded) and tried to link them, you’ll get this warning. You should compile all source files to use the same run-time library. See the Use Run-Time Library (MD, /ML, /MT, /LD) compiler options for more information.

    You can use the linker’s /VERBOSE:LIB switch to determine which libraries the linker is searching. If you receive LNK4098 and want to create an executable file that uses, for example, the single-threaded, non-debug run-time libraries, use the /VERBOSE:LIB option to find out which libraries the linker is searching. The linker should print LIBC.LIB and not LIBCMT.LIB, MSVCRT.LIB, LIBCD.LIB, LIBCMTD.LIB, or MSVCRTD.LIB as the libraries searched. You can tell the linker to ignore the the incorrect run-time libraries by typing the incorrect libraries in the Ignore Libraries text box on the Link tab of the Settings dialog box in Developer’s Studio or by using the /NODEFAULTLIB:library option with LINK for each library you want to ignore. See the Ignore Libraries (/NODEFAULTLIB) linker option for more information.

    The table below shows which libraries should be ignored depending on which run-time library you want to use.

    TABLE 1
    To use this run-time library Ignore these libraries Single-threaded (libc.lib) libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib Multithreaded (libcmt.lib) libc.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib Multithreaded using DLL (msvcrt.lib) libc.lib, libcmt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib Debug Single-threaded (libcd.lib) libc.lib, libcmt.lib, msvcrt.lib, libcmtd.lib, msvcrtd.lib Debug Multithreaded (libcmtd.lib) libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, msvcrtd.lib Debug Multithreaded using DLL (msvcrtd.lib) libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib For example, if you received this warning and you want to create an executable file that uses the non-debug, single-threaded version of the run-time libraries, you could use the following options with the linker:

No comments:

Post a Comment