Statically link to MSVC on Windows

  • platform x86_64-pc-windows-msvc
  • rust v 1.76

I have the following content in my ~/.cargo/config file

[target.x86_64-pc-windows-msvc]
rustflags = [
	"-C", "target-feature=+crt-static",
	"-C", "link-args=/DEFAULTLIB:ucrt.lib /DEFAULTLIB:libvcruntime.lib libcmt.lib",
	"-C", "link-args=/NODEFAULTLIB:libvcruntimed.lib /NODEFAULTLIB:vcruntime.lib /NODEFAULTLIB:vcruntimed.lib",
	"-C", "link-args=/NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:msvcrtd.lib",
	"-C", "link-args=/NODEFAULTLIB:libucrt.lib /NODEFAULTLIB:libucrtd.lib /NODEFAULTLIB:ucrtd.lib",
]

So MSVC runtime is surely statically linked. However, by inspecting the PE file, still there are some non-system DLL are imported.

api-ms-win-crt-string-l1-1-0.dll	14	011E9548	00000000	00000000	011EAD10	00DE3450
api-ms-win-crt-math-l1-1-0.dll	54	011E9230	00000000	00000000	011EAD32	00DE3138
api-ms-win-crt-heap-l1-1-0.dll	8	011E91D8	00000000	00000000	011EAD52	00DE30E0
api-ms-win-crt-runtime-l1-1-0.dll	22	011E93E8	00000000	00000000	011EAD72	00DE32F0
api-ms-win-crt-utility-l1-1-0.dll	2	011E95F8	00000000	00000000	011EAD94	00DE3500
api-ms-win-crt-stdio-l1-1-0.dll	20	011E94A0	00000000	00000000	011EADB6	00DE33A8
api-ms-win-crt-convert-l1-1-0.dll	6	011E9170	00000000	00000000	011EADD6	00DE3078
api-ms-win-crt-time-l1-1-0.dll	6	011E95C0	00000000	00000000	011EADF8	00DE34C8
api-ms-win-crt-filesystem-l1-1-0.dll	3	011E91B8	00000000	00000000	011EAE18	00DE30C0
api-ms-win-crt-environment-l1-1-0.dll	1	011E91A8	00000000	00000000	011EAE3E	00DE30B0
api-ms-win-crt-locale-l1-1-0.dll	1	011E9220	00000000	00000000	011EAE64	00DE3128

How to make them statically linked as well?

Can't help, but for context, here's a related issue on GitHub.

Seems like statically linking crt libraries isn't ideal. And based on the linked discussion you seem to be using correct flags.

1 Like

I cannot use x86_64-pc-windows-gnu because my application links to ffmpeg from vcpkg. x86_64-pc-windows-msvc is the only choice but it is quite annoying if it cannot statically linking to CRT.

The ucrt is bundled with Windows itself on all Windows versions supported by Rust. As such it should be completely fine to dynamically link to it. Unlike msvcrt you don't need to bundle a copy with your own application if you dynamically link against it.

2 Likes

You may want to check the docs for UCRT deployment, in case you may end up relying on too new an API set for the machines your code runs on (TLDR is this shouldn't happen if they're getting Windows updates):


If anyone's curious, this article describes how the ucrt stuff works, and how it keeps abi stability, allowing the system to update it, and still not having issues with different (abi) versions in the same process:

https://mingwpy.github.io/ucrt.html

(The licensing stuff there is sad; I vaguely remember that it's now resolved though? Not including vcruntime.lib, at least)

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.