Rust: Cross compile from windows to linux

Created on 25 May 2017  路  7Comments  路  Source: rust-lang/rust

Environment:

  • Windows 10
  • rustc 1.17.0 (56124baa9 2017-04-24)
  • msys2
  • gcc (Rev2, Built by MSYS2 project) 6.2.0
  • GNU ld (GNU Binutils) 2.27
  • toolchains:
    stable-x86_64-pc-windows-gnu (default)
    stable-x86_64-pc-windows-msvc

I create a test project:

fn main() {
    println!("Hello, world!");
}

When i'm trying build project with command:

cargo build --target=x86_64-unknown-linux-gnu --release --bin test_project

I received an error message:

   Compiling test_project v0.1.0 (file:///D:/Sources/Rust/test_project)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib" "D:\\Sources\\Rust\\test_project\\target\\x86_64-unknown-linux-gnu\\release\\deps\\test_project-a4ab1f96a909babe.0.o" "-o" "D:\\Sources\\Rust\\test_project\\target\\x86_64-unknown-linux-gnu\\release\\deps\\test_project-a4ab1f96a909babe" "-Wl,--gc-sections" "-pie" "-Wl,-O1" "-nodefaultlibs" "-L" "D:\\Sources\\Rust\\test_project\\target\\x86_64-unknown-linux-gnu\\release\\deps" "-L" "D:\\Sources\\Rust\\test_project\\target\\release\\deps" "-L" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libstd-f4594d3e53dcb114.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\librand-1efbcfd8938372b6.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libcollections-532a3dbf317eff86.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libstd_unicode-cfbd6648f7db2ee5.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libpanic_unwind-a0157c0ca919c364.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libunwind-488b4ab4bd53a138.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\liballoc-ca07b617414dd0fa.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\liballoc_jemalloc-492d8ea7fa3384ff.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\liblibc-88c194c15fdb6521.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libcore-687e6a964d22cbb4.rlib" "C:\\Users\\mrant\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-unknown-linux-gnu\\lib\\libcompiler_builtins-987729be881d4d32.rlib" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util"
  = note: D:/Program/Msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: unrecognized option '-z'
          D:/Program/Msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: use the --help option for usage information
          collect2.exe: error: ld returned 1 exit status


error: aborting due to previous error

error: Could not compile `test_project`.

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

I'm not understand what i'm missed

Most helpful comment

If you want to target Linux from Windows, the easiest option is to natively target Linux from within WSL. If you really want to cross compile from Windows without using WSL, then cygwin might have cross compilers you can use.

All 7 comments

Did you remember to get a cross compiling gcc toolchain that targets linux? Remember that MinGW is for targeting Windows, not for cross compiling to Linux from Windows.

Is that possible to get this toolchain? I didn't found any.

I believe cygwin actually has a package for a cross compiler to target linux.

I have a very similar problem, although my error message is could not exec the linker 'cc': The system cannot find the file specified.

I ran rustup target add x86_64-unknown-linux-gnu, that should install a toolchain to compile for Linux, right? And since I ran the command on Windows, it should install a toolchain that can run on Windows?

Adding a rustup target merely adds the rust part of the toolchain. You need a C toolchain that is capable of targeting linux as well. Rustup does not provide the C toolchain that you need.

I see. rustup-init told me I need the Visual C++ Build Tools 2015 to compile for Windows. Is there a similar recommendation to build for Linux?

If you want to target Linux from Windows, the easiest option is to natively target Linux from within WSL. If you really want to cross compile from Windows without using WSL, then cygwin might have cross compilers you can use.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nikomatsakis picture nikomatsakis  路  268Comments

withoutboats picture withoutboats  路  202Comments

thestinger picture thestinger  路  234Comments

withoutboats picture withoutboats  路  213Comments

Leo1003 picture Leo1003  路  898Comments