-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Proposal
Problem statement
With the windows-msvc targets, linking the C runtime can have a number of different configurations. For example, linking the CRT libraries compiled with debug symbols. However, currently the libc
crate (when compiled as a dependency of std) does not allow this. It hardcodes a choice of only two configurations (static or dynamic), controllable with the target-feature
compiler flag.
Motivation, use-cases
When integrating with C/C++ code, it's essential that the same CRT is used for the Rust parts as the C/C++ parts. Mixing CRTs is unsupported. Currently this requires ensuring the C/C++ part of the build to match Rust. The cc
crate can take care of automatically selecting the static or dynamic CRT but this means it can't offer to use debug libraries or other CRTs (because they would be incompatible with Rust).
Even in a pure Rust build, linking the debug CRT may be useful for debugging.
Solution sketches
Add a cfg option to the libc crate so that when compiled with rustc-dep-of-std
the right CRT can be selected. Users can then set rustflags in config.toml
to set the options for a build.
[target.'cfg(all(windows, target_env="msvc"))']
rustflags = [
"-C", "target-feature=+crt-static",
"--cfg", 'msvc_crt_link="debug"',
]
This cfg
can also be detected in build scripts using CARGO_CFG_MSVC_CRT_LINK
so that crates such as cc
can adjust other native builds accordingly. Options include "debug" for linking the debug CRT the and "nocrt" for taking manual control of linking the CRT (essentially equivalent to using nostd
).
Links and related work
- rustc always links against non-debug Windows runtime rust#39016
- [DRAFT] MSVC: Add
msvc_crt_link
options libc#3178
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.