cc-rs: Cannot cross-compile .a file from linux to windows

Given the following Cargo.toml:

[package]
name = "cc-example"
version = "0.1.0"

[build-dependencies]
cc = "1"

build.rs:

extern crate cc;

fn main() {
    cc::Build::new()
        .file("example.c")
        .compile("example.a");
}

and example.c:

int f() {
    return 0;
}

the cc crate will incorrectly try to use the Windows toolchain instead of the host toolchain.

Full output from cargo build
$ cargo build --target x86_64-pc-windows-msvc
   Compiling cc-example v0.1.0 (/home/joshua/src/rust/cc-example)
error: failed to run custom build command for `cc-example v0.1.0 (/home/joshua/src/rust/cc-example)`

Caused by:
  process didn't exit successfully: `/home/joshua/.local/lib/cargo/target/debug/build/cc-example-88984b21b33634e3/build-script-build` (exit code: 1)
--- stdout
TARGET = Some("x86_64-pc-windows-msvc")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-unknown-linux-gnu")
CC_x86_64-pc-windows-msvc = None
CC_x86_64_pc_windows_msvc = None
TARGET_CC = None
CC = None
CROSS_COMPILE = None
CFLAGS_x86_64-pc-windows-msvc = None
CFLAGS_x86_64_pc_windows_msvc = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
running: "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-m64" "-Wall" "-Wextra" "-Fo/home/joshua/.local/lib/cargo/target/x86_64-pc-windows-msvc/debug/build/cc-example-183c547311261870/out/example.o" "-c" "example.c"
exit code: 0
AR_x86_64-pc-windows-msvc = None
AR_x86_64_pc_windows_msvc = None
TARGET_AR = None
AR = None
running: "lib.exe" "-out:/home/joshua/.local/lib/cargo/target/x86_64-pc-windows-msvc/debug/build/cc-example-183c547311261870/out/libexample.a.a" "-nologo" "/home/joshua/.local/lib/cargo/target/x86_64-pc-windows-msvc/debug/build/cc-example-183c547311261870/out/example.o"

--- stderr


error occurred: Failed to find tool. Is `lib.exe` installed?

Note that it correctly detects the host and target platforms, but still tries to use lib.exe instead of ld.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 19 (19 by maintainers)

Most upvoted comments

I don’t mind if this doesn’t work honestly, I just wish the error message were more helpful, rather than looking like a bug in the cc crate.