I wrote an application with Rust, which is working quite fine on Windows and Linux using cargo run and cargo build.
To have only one system capable of compiling for both targets x86_64-unknown-linux-gnu and x86_64-pc-windows-msvc I set up a Ubuntu 22.04 minimum server installation with these additional commands
apt update
apt upgrade -y
apt install curl build-essential gcc make -y
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > ~/rustup.sh
cd ~
chmod u+x rustup.sh
./rustup.sh -y
. ~/.cargo/env
rustup target add x86_64-unknown-linux-gnu --toolchain stable
rustup target add x86_64-pc-windows-msvc --toolchain stable
Then switching to the application directory
cargo build --release --target x86_64-unknown-linux-gnu
works fine and produces the artifact as expected. But
cargo build --release --target x86_64-pc-windows-msvc
failes with the error message
error: failed to run custom build command for `ring v0.16.20`
Caused by:
process didn't exit successfully: `/test/backend/target/release/build/ring-3857f585b2d049fe/build-script-build` (exit status: 101)
--- stdout
OPT_LEVEL = Some("3")
TARGET = Some("x86_64-pc-windows-msvc")
HOST = Some("x86_64-unknown-linux-gnu")
CC_x86_64-pc-windows-msvc = None
CC_x86_64_pc_windows_msvc = None
TARGET_CC = None
CC = None
CROSS_COMPILE = None
CFLAGS_x86_64-pc-windows-msvc = None
CFLAGS_x86_64_pc_windows_msvc = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
--- stderr
running "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-m64" "-I" "include" "-Wall" "-Wextra" "/GS" "/Gy" "/EHsc" "/GR-" "/Zc:wchar_t" "/Zc:forScope" "/Zc:inline" "/Zc:rvalueCast" "/sdl" "/Wall" "/wd4127" "/wd4464" "/wd4514" "/wd4710" "/wd4711" "/wd4820" "/wd5045" "/Ox" "-DNDEBUG" "-c" "/Fo/test/backend/target/x86_64-pc-windows-msvc/release/build/ring-e02c6ae3738202e7/out/aes_nohw.obj" "crypto/fipsmodule/aes/aes_nohw.c"
cc: error: -E or -x required when input is from standard input
thread 'main' panicked at 'execution failed', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/build.rs:656:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
As stated by the ring library I need to
When you build ring for a target that is different than the one you are using for the build process you need to install the rust tool chain and a C/C++ compiler that can produce binaries for the intended target.
Besides the required dependencies you need to set the environment variables TARGET_CC and TARGET_AR to the full path of the cross-compiler and the cross-archiver respectively.
Still I was not able to figure out which package to install and how to set the mentioned variables TARGET_CC and TARGET_AR in order to make the build working.
Is there a way how to cross-compile a Rust-Application for Windows under Ubuntu Linux 22.04 without using a VM or Docker-Container with a Windows installation?