2

I have a simple file main.rs, with the print Hello, World from Rust!. Under my system, Linux, everything compiles without any problems, both through rustc and through cargo. But when I want to make a build for another system, for example macOS, I get an error.

What am I doing:

rustup target add x86_64-apple-darwin
rustc ./main.rs --target=x86_64-apple-darwin

Error:

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/daniilskli/.config/composer/vendor/bin:/home/daniilskli/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/android-sdk/tools:/opt/android-sdk/tools/bin:/opt/cuda/bin:/opt/cuda/nsight_compute:/opt/cuda/nsight_systems/bin:/home/daniilskli/.dotnet/tools:/var/lib/flatpak/exports/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/usr/lib/rustup/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "x86_64" "-m64" "/tmp/rustctIgaJB/symbols.o" "main.main.3f728549c24fce79-cgu.0.rcgu.o" "main.4zckhff98u3ljprx.rcgu.o" "-L" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libstd-6472438448c6c4bd.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-3600ed8016f8face.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libobject-ea51991ef4a3658d.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-96b3ff65689c3fce.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-07fa1dec1d2bd3bb.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libgimli-175fd87b93400a41.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-3b326864ccc9e9e8.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-39dcdc3dac7bf511.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-84429b66c29d7ef9.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-26b6ba85d2896aa6.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libminiz_oxide-3efa691a08d42571.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libadler-81d639bf7b0fb091.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libunwind-f27a894b1cbe21a7.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-1b1ed482c4b1447a.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e263b0a0b4d1d51.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/liballoc-6938fc20e09e5c3a.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-f588925d2f9025bc.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcore-4b6dec082eeb7c38.rlib" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-950dfa4358313a7f.rlib" "-lSystem" "-lc" "-lm" "-L" "/home/daniilskli/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib" "-o" "main" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: cc: error: unrecognized command-line option '-arch'
          

error: aborting due to previous error
3
  • Any reason you're not compiling with cargo? Commented Oct 13, 2023 at 16:20
  • @BallpointBen I use cargo for my projects, just in this case with 1 small file I used rustc. With cargo I get the same error when specifying target as x86_64-apple-darwin Commented Oct 13, 2023 at 17:17
  • You probably need clang for this, cc is generally linked to your system compiler via the system defaults, often GCC. Might be beneficial to specify a path to a JSON file using the --target flag that explicitly names clang as the compiler to use, too. Commented May 25 at 11:28

2 Answers 2

0

As stated in the neighbouring answer, for non-trivial programs with Framework dependencies you'll probably need Mac sysroot extracted from some Mac or SDK.

As far as I remember, trivial programs (e.g. using only stdin/stdout, CLI arguments, based file access) may be buildable without it.

You'll likely need to configure a linker in $HOME/.cargo/config.toml, with something like:

[target."x86_64-apple-darwin"]
linker = "rust-lld"

or

[target."x86_64-apple-darwin"]
linker="/opt/my_linker_for_mac"

, where "my_linker_for_mac" is a gcc/clang-like cross compiler/linker script that can build e.g. C programs for Mac.

An example of such cross compiler may be Zig with appropriate options. Here is my version of such script:

#!/bin/bash
set -e

OPTS=()

for x in "$@" ; do
   if [ "$x" = "-liconv" ]; then
       continue;
   fi
   OPTS+=("$x")
done

exec /opt/zig/zig cc -target x86_64-macos --sysroot=/opt/osxcross/SDK/MacOSX15.1.sdk -F/opt/osxcross/SDK/MacOSX15.1.sdk/System/Library/Frameworks "${OPTS[@]}"

It removes some parts of command line Rust gives to the linker and passes the work to Zig linker (which can build trivial programs for Mac by itself and also some non-trivial programs if you specify the path to sysroot, extracted by osxcross).

This solution is not very stable. You should be ready to solve problems with linking when you add dependencies in your project. The script may require adjustments when Rust changes.

Sign up to request clarification or add additional context in comments.

Comments

-1

Due to licensing reasons, it is not possible to make e.g. Docker images available running on Linux which target macOS (Apple's license does not allow publishing the macOS SDK). If it would be allowed, you could use cross-rs to trivially cross-compile Rust from Linux to macOS. Due to this license restriction, you would need to compile your own Linux->macOS cross-toolchain, and then your command will work.

There are instructions available at https://github.com/tpoechtrager/osxcross explaining how to do this, but you will need to export the macOS SDK yourself, and then compile your own cross-toolchain.

2 Comments

This has nothing to do with docker, licenses, etc.
@Qix-MONICAWASMISTREATED what do you mean? It completely does. I'm the maintainer of a project allowing to generate cross-toolchains Linux -> Darwin, but the reason we can't publish those as binaries is the license of the macOS SDK. This is the reason this can't be made easily available to end-users. For the same reason such a macos cross-toolchain can't be made available in a Docker image which would be used by cross-rs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.