precompiled static library /MT and /MTd error

37 views
Skip to first unread message

ry...@mediapixel.co.nz

unread,
Apr 29, 2019, 7:45:59 AM4/29/19
to bazel-discuss
Error:
LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library

I am trying to depend on libsodium: https://download.libsodium.org/libsodium/releases/libsodium-1.0.17-stable-msvc.zip

Using the following Bazel configuration:

# WORKSPACE
new_local_repository(
name = "org_libsodium_sodium",
build_file = "third_party/sodium.BUILD",
path = "third_party/sodium",
)

# sodium.BUILD
config_setting(
name = "windows_dbg_build",
constraint_values = ["@bazel_tools//platforms:windows"],
values = {"compilation_mode": "dbg"},
)

config_setting(
name = "windows_fastbuild_build",
constraint_values = ["@bazel_tools//platforms:windows"],
values = {"compilation_mode": "fastbuild"},
)

config_setting(
name = "windows_opt_build",
constraint_values = ["@bazel_tools//platforms:windows"],
values = {"compilation_mode": "opt"},
)

cc_library(
name = "sodium",
srcs = select({
":windows_dbg_build": ["lib/dbg/libsodium.lib"],
":windows_fastbuild_build": ["lib/dbg/libsodium.lib"],
":windows_opt_build": ["lib/opt/libsodium.lib"],
"//conditions:default": ["lib/opt/libsodium.a"],
}),
hdrs = glob(["sodium/**/*.h"]),
defines = ["SODIUM_STATIC"],
visibility = ["//visibility:public"],
)

Is it correct to use the /MT and /MTd Runtime Library for Windows precompiled static libraries?

Any idea what I am doing wrong?

Kind regards,
Ryan

mal...@gmail.com

unread,
May 1, 2019, 2:31:04 AM5/1/19
to bazel-discuss
/MT means link to the static C/C++ CRT runtime, e.g. the one that would not use MSVCRxxx.DLL and family.
/MTd is the same, but for debug.

/MD on the other side means link to the MVSCRxxx.DLL dll (for release)
/MDd - same for debug.

Now both (/MT, /MD) version can target DLLs and static libs themselves (if that makes sense),

e.g. you can create a .DLL compiled with /MT, and thus you are bundling a copy of the "CRT" library just for this DLL
and if you are statically linking with /MT against other libs, then they also should be using /MT.

Microsoft added in the last few major versions of the compiler ways to enforce this:

Basically anything compiled with /MT, /MTd, /MD, /MDd (similarly others) would inject
a special "tag" in the .lib, under some name - e.g. a key/value combination. For example
it coudl've done #pragma detect_mismatch("CrtTarget", "MultiThreadedStatic_eg_MT")
and above maybe inserted by the compiler if are doing /MT, then for /MD it may do:
#pragma detect_mismatch("CrtTarget", "MultiThreadedDll_eg_MD")

So you would have two .obj files with same key ("CrtTarget"), but different value,
thus causing mismatch, thus the issue.

There are some other ways to enforce, and warn about this, and the "CrtTarget"
key was made up by me, along the value (but if you dig somewhere in your yvals.h
or neighbours, you may find it)

ry...@mediapixel.co.nz

unread,
May 1, 2019, 6:32:04 AM5/1/19
to bazel-discuss
On Wednesday, 1 May 2019 18:31:04 UTC+12, mal...@gmail.com wrote:
> /MT means link to the static C/C++ CRT runtime, e.g. the one that would not use MSVCRxxx.DLL and family.
> /MTd is the same, but for debug.
>
>
> /MD on the other side means link to the MVSCRxxx.DLL dll (for release)
> /MDd - same for debug.
>
>
> Now both (/MT, /MD) version can target DLLs and static libs themselves (if that makes sense),
>
>
> e.g. you can create a .DLL compiled with /MT, and thus you are bundling a copy of the "CRT" library just for this DLL
> and if you are statically linking with /MT against other libs, then they also should be using /MT.

Ok, this makes a lot more sense to me now thank you for your explanation.
Looking at the Bazel code, /MD is the default.

Is there any way to enforce /MT with Bazel and what advantages does that give,
if any?

Kind regards,
Ryan

Diego

unread,
May 10, 2021, 12:33:01 PMMay 10
to bazel-discuss
Bump! I also need to enforce /MT when generating my windows executables.

Reply all
Reply to author
Forward
0 new messages