Skip to content

Commit

Permalink
Fix to build with C++ environment
Browse files Browse the repository at this point in the history
C++ requires additional runtime to be linked by rust linker.

Reference:
rust-lang/rust#36710 (comment)
  • Loading branch information
blmarket committed Jan 24, 2021
1 parent 8d34cdb commit 15de22f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[target.x86_64-unknown-linux-musl]
linker = "/opt/cross/bin/x86_64-linux-musl-gcc"
rustflags = ["-C", "link-args=-static"]
linker = "./linker"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ rust:

.PHONY: rust-musl
rust-musl:
CXX=/opt/cross/bin/x86_64-linux-musl-g++ cargo run --target x86_64-unknown-linux-musl
CROSS_COMPILE=/opt/cross/bin/x86_64-linux-musl- cargo run --target x86_64-unknown-linux-musl
18 changes: 18 additions & 0 deletions linker
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

args=()

for arg in "$@"; do
if [[ $arg = *"Bdynamic"* ]]; then
args+=() # we do not want this arg
elif [[ $arg = *"crti.o"* ]]; then
args+=("$arg" "/opt/cross/lib/gcc/x86_64-linux-musl/9.2.0/crtbeginS.o" "-Bstatic")
elif [[ $arg = *"crtn.o"* ]]; then
args+=("-lgcc" "-lgcc_eh" "-lc" "/opt/cross/lib/gcc/x86_64-linux-musl/9.2.0/crtendS.o" "$arg")
else
args+=("$arg")
fi
done

echo "RUNNING WITH ARGS: ${args[@]}"
/opt/cross/bin/x86_64-linux-musl-g++ "${args[@]}"

0 comments on commit 15de22f

Please sign in to comment.