Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc on Mac OS X: "unknown option --as-needed" #34282

Closed
cflewis opened this issue Jun 15, 2016 · 9 comments
Closed

rustc on Mac OS X: "unknown option --as-needed" #34282

cflewis opened this issue Jun 15, 2016 · 9 comments

Comments

@cflewis
Copy link

cflewis commented Jun 15, 2016

When trying to cross-compile to a new target (installed by rustup) it looks like --as-needed is spawned by rustc, but this isn't available on Mac OS X (El Capitain)

❯ cargo build --target=mipsel-unknown-linux-gnu -v
   Compiling tessel v0.1.0 (file:///Users/cflewis/Documents/Computing/rust/tessel)
     Running `rustc src/main.rs --crate-name tessel --crate-type bin -g --out-dir /Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug --emit=dep-info,link --target mipsel-unknown-linux-gnu -L dependency=/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug -L dependency=/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug/deps`
error: linking with `cc` failed: exit code: 1
note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-L" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib" "/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug/tessel.0.o" "-o" "/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug/tessel" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug" "-L" "/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug/deps" "-L" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/libstd-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/libcollections-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/librustc_unicode-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/librand-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/liballoc-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/liballoc_jemalloc-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/liblibc-d16b8f0e.rlib" "/Users/cflewis/.multirust/toolchains/stable-x86_64-apple-darwin/lib/rustlib/mipsel-unknown-linux-gnu/lib/libcore-d16b8f0e.rlib" "-l" "dl" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util" "-l" "compiler-rt"
note: clang: warning: argument unused during compilation: '-pie'
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 `tessel`.

Caused by:
  Process didn't exit successfully: `rustc src/main.rs --crate-name tessel --crate-type bin -g --out-dir /Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug --emit=dep-info,link --target mipsel-unknown-linux-gnu -L dependency=/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug -L dependency=/Users/cflewis/Documents/Computing/rust/tessel/target/mipsel-unknown-linux-gnu/debug/deps` (exit code: 101)
@eefriedman
Copy link
Contributor

When you're cross-compiling, you have to point cargo at an appropriate C toolchain, to pick up necessary bits like the correct linker and C runtime. See also http://blog.rust-lang.org/2016/05/13/rustup.html , in particular the discussion of the "linker" option.

@alexcrichton
Copy link
Member

Yeah currently this is expected, albeit unfortunate, behavior. As @eefriedman mentioned the key thing here is finding a linker for mipsel-unknown-linux-gnu which the native system linker doesn't support on OSX.

As this is expected, though, I'm gonna close this, but feel free to ask questions! Unfortunately I've never heard of anyone successfully crossing from OSX to mips Linux, but in theory it's definitely possible!

@samuela
Copy link

samuela commented Mar 10, 2021

Following https://github.com/chinedufn/cross-compile-rust-from-mac-to-linux, the following worked for me:

$ rustup target add x86_64-unknown-linux-gnu
$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu
$ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --target=x86_64-unknown-linux-gnu

@Banyc
Copy link

Banyc commented Apr 15, 2022

It would be great if rustup target add x86_64-unknown-linux-gnu could handle this problem for me.

@yiya1989
Copy link

yiya1989 commented Aug 23, 2022

Following https://github.com/chinedufn/cross-compile-rust-from-mac-to-linux, the following worked for me:

$ rustup target add x86_64-unknown-linux-gnu
$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu
$ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --target=x86_64-unknown-linux-gnu

Thanks for your reply, I tried add these to .cargo/config.toml, it works.

$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu

# .cargo/config.toml
[build]
target = "x86_64-unknown-linux-gnu"

[target.x86_64-unknown-linux-gnu]
linker = "/usr/local/bin/x86_64-unknown-linux-gnu-gcc"

@jiakuan
Copy link

jiakuan commented Oct 9, 2023

Following https://github.com/chinedufn/cross-compile-rust-from-mac-to-linux, the following worked for me:

$ rustup target add x86_64-unknown-linux-gnu
$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu
$ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --target=x86_64-unknown-linux-gnu

Thanks for your reply, I tried add these to .cargo/config.toml, it works.

$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu

# .cargo/config.toml
[build]
target = "x86_64-unknown-linux-gnu"

[target.x86_64-unknown-linux-gnu]
linker = "/usr/local/bin/x86_64-unknown-linux-gnu-gcc"

Thanks for sharing. It worked for me as well, although the linker path is a bit different for me:

[target.x86_64-unknown-linux-gnu]
linker = "/opt/homebrew/bin/x86_64-unknown-linux-gnu-gcc"

@lovelock
Copy link

It would be great if rustup target add x86_64-unknown-linux-gnu could handle this problem for me.

From a newbie's perspective, rustup target add should solve this kind of problems, or why would I add the target😄? BTW, the brew tap above solved my problem too.

@lovelock
Copy link

Following https://github.com/chinedufn/cross-compile-rust-from-mac-to-linux, the following worked for me:

$ rustup target add x86_64-unknown-linux-gnu
$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu
$ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --target=x86_64-unknown-linux-gnu

Thanks for your reply, I tried add these to .cargo/config.toml, it works.

$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu

# .cargo/config.toml
[build]
target = "x86_64-unknown-linux-gnu"

[target.x86_64-unknown-linux-gnu]
linker = "/usr/local/bin/x86_64-unknown-linux-gnu-gcc"

Thanks for sharing. It worked for me as well, although the linker path is a bit different for me:

[target.x86_64-unknown-linux-gnu]
linker = "/opt/homebrew/bin/x86_64-unknown-linux-gnu-gcc"

It's a homebrew issue, don't know it's about homebrew version or macOS version and it confuses me when I write scripts for both my M1 laptop (macOS 13.4) and Intel desktop(macOS 12.6)

@phial3
Copy link

phial3 commented May 26, 2024

cargo build --target x86_64-unknown-linux-gnu on MacOS M2 error:
"ld: unknown option: --as-needed", problems remain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants