Videos
Edit: It was a PAINSTAKING process that took an entire day of failing numerous times but persevering, so I managed to do it. Here's how I did it in the end:
First I of course had to install cross:
$ cargo install cross
Then I installed Docker Desktop and got the daemon running. After that I added Windows as a target with Rustup:
$ rustup target add x86_64-pc-windows-msvc
Then I had to build the Docker image for my target, following the instructions at https://github.com/cross-rs/cross-toolchains.
$ cd /path/to/dir # any directory you prefer $ git clone https://github.com/cross-rs/cross $ cd cross $ git submodule update --init --remote
Then I followed the instructions to configure:
$ cargo xtask configure-crosstool
I was looking to build for x86_64-pc-windows-msvc, so I checked the table in README.md and built the appropriate image with the local tag:
$ cargo build-docker-image x86_64-pc-windows-msvc-cross --tag local
I then created a file called Cross.toml in my project directory and added the following lines to it:
[target.x86_64-pc-windows-msvc] image = "ghcr.io/cross-rs/x86_64-pc-windows-msvc-cross:local"
Finally,
$ cross build --release --target x86_64-pc-windows-msvc --verbose
did the job. To be honest, I have no idea how to use Docker and what it's for, and I was just improvising here, which is probably why it took an entire day. Albeit, I'm very happy I managed to get it working, and the resulting .exe works on Windows (I had my friend test it).
I'm on macOS, and I just wrote my first Rust project. My friend and I are both learning different languages (he's learning Python and I'm learning Rust; dw Rust isn't my first language lol). I want to compile my program into an executable that my friend can run on his Windows machine. I would also like if I could compile for Linux so I can have my dad try it out.
Rust not having a runtime means that it doesn't have a lot of code running as part of the language (for example a garbage collector or bytecode interpreter). It does still need to use operating system primitives (i.e. syscalls), and these are different on MacOS and Linux.
What you want is a cross compiler. If you're using rustup, then installing a cross compiler should be simple:
# Install the toolchain to build Linux x86_64 binaries
rustup target add x86_64-unknown-linux-gnu
Then building is:
cargo build --release --target=x86_64-unknown-linux-gnu
Caveat: I don't have an OS X machine to test this on; please comment or edit to fix this if it works!
Well, it is because Rust has no runtime (unlike e.g. Java's JVM) that you can't just compile code on one OS and expect it to run on a different one; what you are looking for is cross-compilation. I haven't done it in Rust, but from what I can gather you can find relevant information on different cross-compilation Rust strategies on this GitHub repo.
I'm joining a project and I was advised that to compile it, I need to run
cargo build --release --target arm-unknown-linux-gnueabihf
I'm on Mac OS (Sonoma 14.2.1). I know I need an ARM toolchain for this but I'm not sure what I need or, more accurately, where/how to install it.
In the project directory, I have been advised to create `.cargo/config.toml`
with this content
[target.arm-unknown-linux-gnueabihf] linker = "gcc-ar-13"
I assume I need to change the value for the linker but, as mentioned, I'm unsure what option to put there.
If it matters, I have no issue with cargo build --release by itself.
Here are more details from the output. I recognize in this case that it seems to be issues with the command line arguments but I dont think this is the right linker so Im not too concerned about this specifically. This is lightly redacted
Compiling cfg-if v1.0.0
# Omitting lots of lines that start with "Compiling"
Compiling project v0.1.19 (/Users/USER/repos/project)
error: linking with `gcc-ar-13` failed: exit status: 1
|
= note: LC_ALL="C" PATH="PATH_REDACTED" VSLANG="1033" "gcc-ar-13" "/var/folders/3z/z7gj1knj2pqf_q17jz7yzc3h0000gq/T/rustcNChRuA/symbols.o" "/Users/USER/repos/project/target/arm-unknown-linux-gnueabihf/release/deps/project-f18b9ac4a98f6089.project.f25b3891c1c0fef8-cgu.15.rcgu.o" "-Wl,--as-needed" "-L" "/Users/USER/repos/project/target/arm-unknown-linux-gnueabihf/release/deps" "-L" "/Users/USER/repos/project/target/release/deps" "-L" "/Users/USER/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/arm-unknown-linux-gnueabihf/lib" "-Wl,-Bstatic" "/Users/USER/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/arm-unknown-linux-gnueabihf/lib/libcompiler_builtins-31fb99244964ed82.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/Users/USER/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/arm-unknown-linux-gnueabihf/lib" "-o" "/Users/USER/repos/project/target/arm-unknown-linux-gnueabihf/release/deps/project-f18b9ac4a98f6089" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: /Library/Developer/CommandLineTools/usr/bin/ar: illegal option -- /
usage:
ar -d [-TLsv] archive file ...
ar -m [-TLsv] archive file ...
ar -m [-abiTLsv] position archive file ...
ar -p [-TLsv] archive [file ...]
ar -q [-cTLsv] archive file ...
ar -r [-cuTLsv] archive file ...
ar -r [-abciuTLsv] position archive file ...
ar -t [-TLsv] archive [file ...]
ar -x [-ouTLsv] archive [file ...]
error: could not compile `project` (bin "project") due to previous errorI had just posted how to cross compile a gtk program from linux for windows, so thought I'd try to figure out the same process to compile to Mac. After I got part way through cross compiling gtk, I realized you need to sign gui apps for them to run in OSX, and have to do that from a mac anyway. So this process is kind of pointless for that. If you are developing a cli tool though, this can still be useful.
first install the toolchain
rustup target add x86_64-apple-darwin
setup the linker
~/.cargo/config
[target.x86_64-apple-darwin] linker = "x86_64-apple-darwin15-gcc" ar = "x86_64-apple-darwin15-ar"
install osxcross. taken from the aur pkgbuild for osxcross-git, you can just install that if you're on arch, but you'll need to modify the pkgbuild and set OSX_MIN_VERSION to 10.7.
git clone https://github.com/tpoechtrager/osxcross cd osxcross wget https://s3.dockerproject.org/darwin/v2/MacOSX10.11.sdk.tar.xz mv MacOSX10.11.sdk.tar.xz tarballs/ sed -i -e 's|-march=native||g' build_clang.sh wrapper/build.sh UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh sudo mkdir -p /usr/local/osx-ndk-x86 sudo mv target/* /usr/local/osx-ndk-x86
then to compile
export PATH=/usr/local/osx-ndk-x86/bin:$PATH export PKG_CONFIG_ALLOW_CROSS=1 cargo build --target=x86_64-apple-darwin --release
if anyone wants to try to get gtk working you can see the steps I took to build it here, but I'm going to give up on it since you can't sign from linux.