Cross-compilation to x86_64-unknown-linux-gnu fails on Mac OSX - linux

I tried to compile one of my Rust projects to the x86_64-unknown-linux-gnu target:
$ cargo build --target=x86_64-unknown-linux-gnu
Compiling deployer v0.1.0 (file:///Users/raphael/web/deployer)
error: linking with `cc` failed: exit code: 1
|
= note: "cc"
= 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)
I don't know what to do with such a message. What should I do to make it work?
Here is my Cargo.toml file:
[package]
name = "deployer"
version = "0.1.0"
authors = ["..."]
[dependencies]
clap = "2.14.0"
time = "0.1.35"
slack-hook = "0.2"
Cargo version:
cargo 0.13.0-nightly (109cb7c 2016-08-19)
Rust version:
rustc 1.12.0 (3191fbae9 2016-09-23)
I tried to update everything with rustup, but I still get the same problem.

Inspired from cross-compile-rust-from-mac-to-linux, I typically install these dependencies to cross compile rust from Mac OS to Linux (e.g. for Docker containers):
rustup target add x86_64-unknown-linux-gnu
# Install a pre-built cross compiler
brew tap SergioBenitez/osxct
brew install x86_64-unknown-linux-gnu
And finally I can cross compile to Linux on Mac OS with:
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc \
cargo build --target=x86_64-unknown-linux-gnu

Related

Cross compile in mac m1 - build for x86_64 linux

I want to build cdylib crate of my repo https://github.com/doki23/prql-java in my mac m1.
My operations are:
rustup target add x86_64-unknown-linux-gnu
cargo build --target x86_64-unknown-linux-gnu
but I met the problem:
= note: ld: unknown option: --version-script=/var/folders/df/1lz9t40n171_5550cy_f1h3c0000gn/T/rustczqz5TV/list
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How to build a x86_64 linux lib in mac m1?
You need to configure the proper linker and ar paths:
Add the below lines to your ${HOME}/.cargo/config:
[target.x86_64-unknown-linux-gnu]
ar = "[PATH_TO_CROSS_TOOLCHAIN]/bin/ar"
linker = "[PATH_TO_CROSS_TOOLCHAIN]/bin/clang"
You can also use the ld of the cross toolchain instead of clang if you have, say gcc.
Assuming that you want to build for android x86_64;
Add this to your ${HOME}/.cargo/config file
[target.x86_64-linux-android]
ar = "${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android-ar"
linker = "/${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin/x86_64-linux-android28-clang"

Error: linking with `cc` failed: exit code: 1 for cargo run

I'm trying to run cargo run a simple hello world application on a M1 mac, and I got the following errors:
Error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-m64" "-arch" "x86_64" "-L" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.0.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.1.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.10.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.11.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.12.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.13.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.14.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.15.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.2.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.3.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.4.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.5.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.6.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.7.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.8.rcgu.o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.build_script_build.d32uozjy-cgu.9.rcgu.o" "-o" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/build/libc-9d36948631c986a4/build_script_build-9d36948631c986a4.52gec2a5zk7hme9a.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/hugosum/Desktop/portfolio-2021/backend/target/debug/deps" "-L" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libstd-351eea491abe4434.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-5aaa4a9e35148ca2.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libobject-3c363f1c75138766.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-80cce18fbd9a62fa.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libgimli-b559ce52f8e8c310.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-bacdd33101496d8b.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-50c1e0544758a9b9.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-400cf8ef953fa406.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libunwind-d523776aee41e7a3.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-0d288267983a5503.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/liblibc-5768a81ce4924201.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/liballoc-f050e0eba5670fa0.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-618d97e25269c450.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libcore-f02aead32a1ff31d.rlib" "/nix/store/i693mglb2d92m1ycmq5vcsn02xgpra9d-rustc-1.52.1/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-07b5b6ec8d6718bf.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-undefined" "dynamic_lookup"
= note: ld: library not found for -liconv
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
error: aborting due to previous error
error: aborting due to previous error
error: could not compile `futures-channel`
error: build failed
I have installed cargo and rustc through nix, and I have tried to check for the cc bin
❯ cc -v
clang version 7.1.0 (tags/RELEASE_710/final)
Target: x86_64-apple-darwin20.5.0
Thread model: posix
InstalledDir: /nix/store/rjy3p4j55ac4vyrmg18w2wrjgpfhsh4r-clang-7.1.0/bin
How can I fix this error?
This is still the first Google search result for "iconv cargo nix", so I'll post the solution I found here.
I am unsure whether or not this is intended or a bug in nixpkgs, but this error pops up because libiconv isn't included in the nix linker flags by default (you can check in $NIX_LDFLAGS). To include it, explicitly install it, e.g. via nix-shell:
# ↓↓↓↓↓↓↓↓
nix-shell -p libiconv -p cargo -p rustc -p rust-analyzer -p rustfmt -p clippy

Running ghc fails with message about iOS simulator

I tried running ghc to compile a haskell program, and I get the following error:
$ ghc --make main
Linking main ...
ld: building for macOS, but linking in object file built for iOS Simulator, file 'main.o' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`clang' failed in phase `Linker'. (Exit code: 1)
Here are some details about my machine:
macOS Catalina 10.15.7
Xcode 12.2
The Xcode developer tools were installed with xcode-select --install
I installed ghc with brew install ghc
Some other output that might be relevant:
$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
If I run ghc --make main -v, this is what the output looks like right before it fails (apologies for the long line, not sure how to format this better):
clang -o main -fno-common -U__PIC__ -D__PIC__ -Wl,-no_compact_unwind main.o -L/usr/local/Cellar/ghc/8.10.1_2/lib/ghc-8.10.1/base-4.14.0.0 -L/usr/local/Cellar/ghc/8.10.1_2/lib/ghc-8.10.1/integer-gmp-1.0.3.0 -L/usr/local/Cellar/ghc/8.10.1_2/libexec/integer-gmp/lib -L/usr/local/Cellar/ghc/8.10.1_2/lib/ghc-8.10.1/ghc-prim-0.6.1 -L/usr/local/Cellar/ghc/8.10.1_2/lib/ghc-8.10.1/rts /var/folders/5j/wwtlv8lx0m5fmhc3hy5w5_000000gp/T/ghc49162_0/ghc_2.o -Wl,-u,_base_GHCziTopHandler_runIO_closure -Wl,-u,_base_GHCziTopHandler_runNonIO_closure -Wl,-u,_ghczmprim_GHCziTuple_Z0T_closure -Wl,-u,_ghczmprim_GHCziTypes_True_closure -Wl,-u,_ghczmprim_GHCziTypes_False_closure -Wl,-u,_base_GHCziPack_unpackCString_closure -Wl,-u,_base_GHCziWeak_runFinalizzerBatch_closure -Wl,-u,_base_GHCziIOziException_stackOverflow_closure -Wl,-u,_base_GHCziIOziException_heapOverflow_closure -Wl,-u,_base_GHCziIOziException_allocationLimitExceeded_closure -Wl,-u,_base_GHCziIOziException_blockedIndefinitelyOnMVar_closure -Wl,-u,_base_GHCziIOziException_blockedIndefinitelyOnSTM_closure -Wl,-u,_base_GHCziIOziException_cannotCompactFunction_closure -Wl,-u,_base_GHCziIOziException_cannotCompactPinned_closure -Wl,-u,_base_GHCziIOziException_cannotCompactMutable_closure -Wl,-u,_base_ControlziExceptionziBase_absentSumFieldError_closure -Wl,-u,_base_ControlziExceptionziBase_nonTermination_closure -Wl,-u,_base_ControlziExceptionziBase_nestedAtomically_closure -Wl,-u,_base_GHCziEventziThread_blockedOnBadFD_closure -Wl,-u,_base_GHCziConcziSync_runSparks_closure -Wl,-u,_base_GHCziConcziIO_ensureIOManagerIsRunning_closure -Wl,-u,_base_GHCziConcziIO_ioManagerCapabilitiesChanged_closure -Wl,-u,_base_GHCziConcziSignal_runHandlersPtr_closure -Wl,-u,_base_GHCziTopHandler_flushStdHandles_closure -Wl,-u,_base_GHCziTopHandler_runMainIO_closure -Wl,-u,_ghczmprim_GHCziTypes_Czh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Izh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Fzh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Dzh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Wzh_con_info -Wl,-u,_base_GHCziPtr_Ptr_con_info -Wl,-u,_base_GHCziPtr_FunPtr_con_info -Wl,-u,_base_GHCziInt_I8zh_con_info -Wl,-u,_base_GHCziInt_I16zh_con_info -Wl,-u,_base_GHCziInt_I32zh_con_info -Wl,-u,_base_GHCziInt_I64zh_con_info -Wl,-u,_base_GHCziWord_W8zh_con_info -Wl,-u,_base_GHCziWord_W16zh_con_info -Wl,-u,_base_GHCziWord_W32zh_con_info -Wl,-u,_base_GHCziWord_W64zh_con_info -Wl,-u,_base_GHCziStable_StablePtr_con_info -Wl,-u,_hs_atomic_add8 -Wl,-u,_hs_atomic_add16 -Wl,-u,_hs_atomic_add32 -Wl,-u,_hs_atomic_add64 -Wl,-u,_hs_atomic_sub8 -Wl,-u,_hs_atomic_sub16 -Wl,-u,_hs_atomic_sub32 -Wl,-u,_hs_atomic_sub64 -Wl,-u,_hs_atomic_and8 -Wl,-u,_hs_atomic_and16 -Wl,-u,_hs_atomic_and32 -Wl,-u,_hs_atomic_and64 -Wl,-u,_hs_atomic_nand8 -Wl,-u,_hs_atomic_nand16 -Wl,-u,_hs_atomic_nand32 -Wl,-u,_hs_atomic_nand64 -Wl,-u,_hs_atomic_or8 -Wl,-u,_hs_atomic_or16 -Wl,-u,_hs_atomic_or32 -Wl,-u,_hs_atomic_or64 -Wl,-u,_hs_atomic_xor8 -Wl,-u,_hs_atomic_xor16 -Wl,-u,_hs_atomic_xor32 -Wl,-u,_hs_atomic_xor64 -Wl,-u,_hs_cmpxchg8 -Wl,-u,_hs_cmpxchg16 -Wl,-u,_hs_cmpxchg32 -Wl,-u,_hs_cmpxchg64 -Wl,-u,_hs_atomicread8 -Wl,-u,_hs_atomicread16 -Wl,-u,_hs_atomicread32 -Wl,-u,_hs_atomicread64 -Wl,-u,_hs_atomicwrite8 -Wl,-u,_hs_atomicwrite16 -Wl,-u,_hs_atomicwrite32 -Wl,-u,_hs_atomicwrite64 -Wl,-search_paths_first -Wl,-dead_strip -lHSbase-4.14.0.0 -lHSinteger-gmp-1.0.3.0 -lHSghc-prim-0.6.1 -lHSrts -lCffi -liconv -lgmp -lm -ldl -Wl,-dead_strip_dylibs
ld: building for macOS, but linking in object file built for iOS Simulator, file 'main.o' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

pip install fastapi[all] is failing due to a rust component?

Hi I'm trying to install fastapi, but it seems to be failing when im installing orjson
Here is the full error code:
Building wheel for orjson (PEP 517) ... error
ERROR: Command errored out with exit status 1:
command: 'd:\pythonapps\fastapi_tutorial\env\scripts\python.exe' 'd:\pythonapps\fastapi_tutorial\env\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmp4yope0de'
cwd: C:\Users\User\AppData\Local\Temp\pip-install-35a1h4do\orjson
Complete output (50 lines):
Compiling cfg-if v0.1.10
Compiling winapi v0.3.8
Compiling bitflags v1.2.1
Compiling ryu v1.0.5
Compiling scopeguard v1.1.0
Compiling packed_simd v0.3.3
Compiling serde v1.0.111
Compiling lexical-core v0.7.4
Compiling libc v0.2.71
Compiling version_check v0.9.2
Compiling smallvec v1.4.0
Compiling arrayvec v0.5.1
Compiling encoding_rs v0.8.23
Compiling static_assertions v1.1.0
Compiling itoa v0.4.5
Compiling orjson v3.1.0 (C:\Users\User\AppData\Local\Temp\pip-install-35a1h4do\orjson)
Compiling rand_core v0.4.2
Compiling inlinable_string v0.1.11
Compiling associative-cache v1.0.1
Compiling once_cell v1.4.0
Compiling lock_api v0.3.4
Compiling wyhash v0.3.0
Compiling pyo3 v0.10.1
error: failed to run custom build command for `pyo3 v0.10.1`
Caused by:
process didn't exit successfully: `C:\Users\User\AppData\Local\Temp\pip-install-35a1h4do\orjson\target\release\build\pyo3-dde0edb355843ae5\build-script-build` (exit code: 1)
--- stderr
Error: "Your Rust target architecture (64-bit) does not match your python interpreter (32-bit)"
warning: build failed, waiting for other jobs to finish...
thread 'rustc' panicked at 'Box<Any>', src\librustc_errors\lib.rs:912:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: rustc 1.46.0-nightly (4fb54ed48 2020-06-14) running on x86_64-pc-windows-msvc
note: compiler flags: -Z mutable-noalias -C opt-level=3 -C panic=abort -C linker-plugin-lto -C codegen-units=1 --crate-type lib
note: some of the compiler flags provided by cargo are hidden
error: build failed
💥 maturin failed
Caused by: Failed to build a native library through cargo
Caused by: Cargo build finished with "exit code: 101": `cargo rustc --message-format json --manifest-path Cargo.toml --lib --release -- -C link-arg=-s`
Running `maturin pep517 build-wheel -i python --manylinux=off --strip=on`
Error: Command '['maturin', 'pep517', 'build-wheel', '-i', 'python', '--manylinux=off', '--strip=on']' returned non-zero exit status 1.
----------------------------------------
ERROR: Failed building wheel for orjson
Failed to build orjson
ERROR: Could not build wheels for orjson which use PEP 517 and cannot be installed directly
Please let me know what could I do? I have installed rustup and set it to nightly. Any help is greatly appreciated Thank you!
The problem is quite clear:
Your Rust target architecture (64-bit) does not match your python interpreter (32-bit)
Either upgrade your Python interpreter to 64 bit, or use rustup to install a 32 bit Rust target
Edit: Here is how you could manually target 32 bit on a x86_64 machine

"Error loading target specification" when building a WebAssembly module with Cargo

I've constructed a simple Rust app as follows:
cargo new hello_world --bin
I can compile to WebAssembly using the Rust compiler as follows:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
I'd like to use Cargo to manage external dependencies, but when I build for the same target using cargo:
cargo build --release --target wasm32-unknown-unknown
It fails as follows:
$ cargo build --release --target wasm32-unknown-unknown
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target wasm32-unknown-unknown --crate-type bin --crate-type rlib` (exit code: 101)
--- stderr
error: Error loading target specification: Could not find specification for target "wasm32-unknown-unknown"
|
= help: Use `--print target-list` for a list of built-in targets
As the wasm32-unknown-unknown target has only recently been added to Rust, it's only available in the nightly toolchain. You have to specify that you wish to use the nightly toolchain:
cargo +nightly build --release --target wasm32-unknown-unknown
# ^^^^^^^^
You were already doing this when calling rustc directly:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
# ^^^^^^^^
It's probably easier if you:
Use a toolchain override in your project directory:
$ rustup override set nightly
Set a default target in your project's .cargo/config:
[build]
target = "wasm32-unknown-unknown"
Then you can just cargo build --release. (It's recommended to avoid debug mode for now).

Resources