How to build centos program on rust mac?

I am a novice and want to deploy the program written to Centos server.

Find the information and say, execute the following command.

rustup target add x86_64-unknown-linux-musl # First time only
cargo build --release --target x86_64-unknown-linux-musl

When I execute it, the following error appears:

xxxx@xx rust_hello % cargo  build --target x86_64-unknown-linux-musl --release
   Compiling rust_hello v0.1.0 (/Volumes/data/rust_hello)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-nostartfiles" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "-L" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps/rust_hello-2cb0146f93d23d46.rust_hello.bxpzb21w-cgu.0.rcgu.o" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps/rust_hello-2cb0146f93d23d46.rust_hello.bxpzb21w-cgu.1.rcgu.o" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps/rust_hello-2cb0146f93d23d46.rust_hello.bxpzb21w-cgu.2.rcgu.o" "-o" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps/rust_hello-2cb0146f93d23d46" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps/rust_hello-2cb0146f93d23d46.2pxdl82tzlmdajwy.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/Volumes/data/rust_hello/target/x86_64-unknown-linux-musl/release/deps" "-L" "/Volumes/data/rust_hello/target/release/deps" "-L" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-d2918ad39560ef70.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-f4ca16729b67f4dd.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-400395ea2841002a.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-b0533ea0af675dc9.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-e9d03d8a1cf7a110.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-fe8cb29709d122d4.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-09ef970e0d183722.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-1a8801c99e79f10f.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-886c89b28e5e0865.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-dcf93364faa2ceaa.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-75081e895819d734.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-6c4598f129413e8a.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-d3a472b733fbfd21.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-2f23acd805819d02.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-d2b457edb67a2174.rlib" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-fc65d219541fe932.rlib" "-Wl,--end-group" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-f4df6b79d8e73f02.rlib" "-Wl,-Bdynamic" "/Users/xxxx/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: clang: warning: argument unused during compilation: '-static-pie' [-Wunused-command-line-argument]
          ld: unknown option: --as-needed
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `rust_hello`

To learn more, run the command again with --verbose.

Rust itself is incapable of cross-compiling anything other than static libraries. Rust relies on host system's linker, but macOS linker can't link Linux executables and doesn't ship with musl installed.

You need to install cross-compilation tools for musl, including a musl-compatible linker and Linux system libraries.

Personally, I found mac->linux cross-compilation broken and annoying. It's much less painful to use a Linux virtual machine and compile for musl there.

1 Like

To the answer provided by @kornel I'd like to add that in general you're going to want a separate host (virtual or not) for each compilation target.

Yes the number of hosts will add up, but as far as I can see it's currently the easiest route to target multiple OSes and chip architectures.

I'd also like to add that in my travels i've come across Notes on cross-compiling Rust which suggests that you can download a clang+llvm tarball that provides an lld that may work. Note that I haven't attempted this or verified that the suggestions in the blog work so YMMV.

https://github.com/emk/rust-musl-builder

Personally I use docker containers to compile for linux from mac. It seems generally easier than setting up cross compilation.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.