Can't get static linking to work

Roger1297 1 Reputation point
2020-11-29T16:25:06.257+00:00

I have a simple solution testing modules using MSVC in C++20 preview features.
It works fine with dynamic linking, but if I change code generation to /MT or /MTd it won't link.

There are no external libraries outside the solution.
There is a static lib with modules that links correctly.
The test application fails.

The errors start with MSVCRT which doesn't seem very static.

How do I link statically?

The errors start with:
2>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
2>LINK : warning LNK4217: symbol '_invalid_parameter_noinfo_noreturn' defined in 'libucrt.lib(invalid_parameter.obj)' is imported by 'std.lib(filesystem.obj)' in function '"void * __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)" (??$_Allocate_manually_vector_aligned@U_Default_allocate_traits@std@@@std@@YAPEAX_K@Z)'
2>LINK : warning LNK4217: symbol 'ceil' defined in 'libucrt.lib(ceil.obj)' is imported by 'std.lib(filesystem.obj)' in function 'ceill'
2>LINK : warning LNK4217: symbol 'calloc' defined in 'libucrt.lib(calloc.obj)' is imported by 'std.lib(filesystem.obj)' in function '"char * __cdecl std::_Maklocstr<char>(char const *,char *,struct _Cvtvec const &)" (??$_Maklocstr@D@std@@YAPEADPEBDPEADAEBU_Cvtvec@@@Z)'
2>LINK : warning LNK4217: symbol 'free' defined in 'libucrt.lib(free.obj)' is imported by 'std.lib(filesystem.obj)' in function '_freea'
2>LINK : warning LNK4286: symbol 'free' defined in 'libucrt.lib(free.obj)' is imported by 'msvcprt.lib(filesystem.obj)'
2>LINK : warning LNK4217: symbol '_errno' defined in 'libucrt.lib(errno.obj)' is imported by 'std.lib(filesystem.obj)' in function '"double __cdecl std::_Stodx_v2(char const *,char * *,int,int *)" (?_Stodx_v2@std@@YANPEBDPEAPEADHPEAH@Z)'
2>LINK : warning LNK4217: symbol '__acrt_iob_func' defined in 'libucrt.lib(_file.obj)' is imported by 'std.lib(filesystem.obj)' in function '_printf_l'
2>LINK : warning LNK4217: symbol '__stdio_common_vfwprintf' defined in 'libucrt.lib(output.obj)' is imported by 'std.lib(filesystem.obj)' in function '_vfwprintf_l'

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 9,181 Reputation points Microsoft Vendor
    2020-11-30T01:58:39.003+00:00

    Hi,

    There are 4 versions of the CRT link libraries : CRT Library Features

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

    Linker Tools Warning LNK4098 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.

    For example, when your executable uses the multi-threaded, non-debug run-time libraries, the list reported should include LIBCMT.lib, and not LIBCMTD.lib, MSVCRT.lib, or MSVCRTD.lib. You can tell the linker to ignore the incorrect run-time libraries by using /NODEFAULTLIB for each library you want to ignore.

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.