Skip to content

Commit

Permalink
link to debug c runtime on Windows when build with debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Jun 21, 2024
1 parent 0804753 commit dcf83cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 14 additions & 1 deletion doc/static-linking.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Static Linking
Static Linking
---

# Linux

## Install musl

Expand All @@ -25,3 +28,13 @@ Then compile with the features that do not require dynamic linking:
```shell
cargo build --target=x86_64-unknown-linux-musl --no-default-features --features vendored-openssl,vendored-c-ares
```

# Windows

Windows provides both dynamic and static C runtimes.

See [C runtime (CRT) and C++ standard library (STL) .lib files](https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features).

You can change to use a static runtime by setting `target-feature=+crt-static` rustc flag.

See [Static and dynamic C runtimes](https://doc.rust-lang.org/reference/linkage.html#static-and-dynamic-c-runtimes).
22 changes: 18 additions & 4 deletions lib/g3-build-env/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ pub fn check_basic() {
"cargo:rustc-env=G3_BUILD_OPT_LEVEL={}",
env::var("OPT_LEVEL").unwrap()
);
println!(
"cargo:rustc-env=G3_BUILD_DEBUG={}",
env::var("DEBUG").unwrap()
);
let debug = env::var("DEBUG").unwrap();
println!("cargo:rustc-env=G3_BUILD_DEBUG={debug}");
#[cfg(target_env = "msvc")]
if debug == "true" {
// Change the C Runtime Type
// See https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-warning-lnk4098
// FIXME switch to UCRT (ucrt(d) for DLL and libucrt(d) for static) ?
let linkage = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
if linkage.contains("crt-static") {
println!("cargo:rustc-link-arg-bins=/nodefaultlib:libcmt");
println!("cargo:rustc-link-arg-bins=/nodefaultlib:msvcrtd");
println!("cargo:rustc-link-lib=dylib=libcmtd");
} else {
// See https://learn.microsoft.com/en-us/cpp/porting/upgrade-your-code-to-the-universal-crt
println!("cargo:rustc-link-arg-bins=/nodefaultlib:msvcrt");
println!("cargo:rustc-link-lib=dylib=msvcrtd");
}
}

if let Ok(v) = env::var("G3_PACKAGE_VERSION") {
println!("cargo:rustc-env=G3_PACKAGE_VERSION={v}");
Expand Down

0 comments on commit dcf83cb

Please sign in to comment.