Why do I receive a LNK4098 warning about conflicting libraries when compiling a MEX-file?

11 views (last 30 days)
I have written a MEX-file that links to external Microsoft libraries. When I compile the MEX-function with verbose output, I receive the following warning:
LINK : warning LNK4098: defaultlib "libc.lib" conflicts with use of
other libs; use /NODEFAULTLIB:library

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Jan 2017
The following Microsoft document discusses the source of this warning:
In particular, this page states:
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.
Furthermore, the following Microsoft page explains Microsoft compile switches:
This page states the following for the /ML switch:
Causes the compiler to place the library name LIBC.LIB into the .OBJ file so that the linker will use LIBC.LIB to resolve external symbols. This is the compiler’s default action. LIBC.LIB does not provide multithread support.
Note that MATLAB uses either /MD (Multithreaded DLL, nondebug) or /MDd (Multithreaded DLL, debug) to compile MEX-functions. As a result, the warning may be generated if your MEX-file is linked to a library that was compiled using the default /ML switch instead of /MDd (or /MD for regular MEX-files).
You may need to recompile the external libraries with the /MDd switch (and the /MD switch for non-debug MEX-files). Alternatively, you might add the line at the bottom of the Microsoft document to your compiler options file. For example, you would add the following to the line that starts "set LINKFLAGS=":
/NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcd.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib
You can also override the LINKFLAGS variable by using the <name>#<value> switch of MEX. For details on how to use this flag, execute:
help mex

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!