Skip to content

Windows MSVC CRT linking #211

@ChrisDenton

Description

@ChrisDenton

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions