Programming Languages Rust Subjective
Oct 04, 2025

How do you handle cross-compilation in Rust?

Detailed Explanation
Cross-compilation in Rust: • Target triple specification (arch-vendor-os) • Toolchain installation • Linker configuration • Platform-specific dependencies • Conditional compilation Common targets: • x86_64-pc-windows-gnu (Windows) • x86_64-apple-darwin (macOS) • x86_64-unknown-linux-gnu (Linux) • aarch64-apple-darwin (Apple Silicon) • wasm32-unknown-unknown (WebAssembly) Setup process: # Install target rustup target add x86_64-pc-windows-gnu # Install cross-compilation tools # For Windows on Linux: sudo apt-get install gcc-mingw-w64 # Configure linker in .cargo/config.toml [target.x86_64-pc-windows-gnu] linker = "x86_64-w64-mingw32-gcc" Build commands: cargo build --target x86_64-pc-windows-gnu cargo build --target wasm32-unknown-unknown Conditional compilation: #[cfg(target_os = "windows")] fn platform_specific() { println!("Running on Windows"); } #[cfg(target_os = "linux")] fn platform_specific() { println!("Running on Linux"); } #[cfg(target_arch = "wasm32")] fn wasm_specific() { // WebAssembly specific code } Cargo.toml dependencies: [target.'cfg(windows)'.dependencies] winapi = "0.3" [target.'cfg(unix)'.dependencies] libc = "0.2"
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback