Cross-compile on macOS for Windows

I found these two posts where they ask about cross compiling for Windows on MacOS:

About a year ago I asked an LLM about it, and it uttered something that worked well for me (I did two smallish projects where I used it - both console, without GUI). As in the above posts I didn't see these simple few steps, I thought I post them here:

  1. Add Windows as a Target
    rustup target add x86_64-pc-windows-gnu

  2. Install Windows Linker
    brew install mingw-w64

  3. Create a .cargo/config.toml Configuration
    target.x86_64-pc-windows-gnu]
    linker = "x86_64-w64-mingw32-gcc"

  4. Build for Windows
    cargo build --release --target x86_64-pc-windows-gnu

The compiled executable will be in target/x86_64-pc-windows-gnu/release/test.exe

Important Notes:

  • Make sure a config.toml file uses Windows-style line endings (CRLF)
  • Path separators in the code should handle both Unix and Windows styles
  • File operations might need adjustments for Windows paths
  • Consider using the (std::path::PathBuf) type for cross-platform path handling
  • about installing brew see here: https://brew.sh
1 Like

No need for that. Cargo, rustc and the rust standatd library accept both LF and CRLF as line terminator on all platforms.

1 Like