cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Building binaries using clang 16 on macOS results in linker error

Open cirla opened this issue 2 years ago • 0 comments

Description

When building a binary rust project in a workspace with a crate that was using cxx, I encountered linker errors for missing libstdc++ symbols when building with clang 16 that I had not encountered in older versions of clang.

...
  = note: Undefined symbols for architecture arm64:
            "___gxx_personality_v0", referenced from:
                void rust::cxxbridge1::panic<std::invalid_argument>(char const*) in libcxx-7d0d833a32f757b8.rlib(cxx.o)
...

I thought this was odd as I noticed "-lc++" present in the cargo linker line, yet the linker seemed to not be operating as clang++, but rather clang.

Additional Info

  • macOS 14.2.1 (23C71)
[[package]]
name = "cxx"
version = "1.0.107"
...
[[package]]
name = "cxx-build"
version = "1.0.107"
...
❯ cc --version
clang version 16.0.6
Target: arm64-apple-darwin
Thread model: posix
InstalledDir: /nix/store/a5v30qll5i02vr9y97bk1rdx3mm6kvlm-clang-16.0.6/bin

Workaround

I was able to successfully link the binary by setting RUSTFLAGS="-C link-args=-xc++" or adding the following to .cargo/config.toml:

[target.aarch64-apple-darwin]
# lld not necessary for workaround
rustflags = ["-C", "link-arg=-fuse-ld=lld", "-C", "link-arg=-xc++"]

What Didn't Work

Using .cpp(true) or println!("cargo:rustc-link-arg=-xc++"); in the build.rs for the crate using cxx, as the linker issue was only encountered when building the binary which depended on that crate.

cirla avatar Jan 18 '24 19:01 cirla