please write a list of points from this section of the book, and clean up the…
- User
please write a list of points from this section of the book, and clean up the explanations:
10.2.2 Cross-compile your binary for different environments The binary size is not the only consideration when compiling Rust code. During com- pilation, the compiler uses operating system libraries to build the binary. Each build is dependent on the underlying API calls to the operating system it is running on. If you are on macOS, the binary you are producing will also run on macOS. If you try to copy the code over to a Linux machine and run it, it will fail. Rust supports the compilation for various environments, called targets (http://mng .bz/DDBE). You can get a list of available targets via the commandrustuptargetlist: $ rustup target list aarch64-apple-darwin (installed) aarch64-apple-ios aarch64-apple-ios-sim aarch64-fuchsia aarch64-linux-android ...
A full list can be found via the Platform Support section of the Rustc documentation website (http://mng.bz/lR8y). You have to first add an additional target to your tool- chain via Rustup before you can build your codebase for this specific environment. So, if you are on a macOS M1 machine and want to build a binary for an Intel Mac, you have to add the target first and then specify it at the build step: cargo build --release --target x86_64-apple-darwin Another common use case is to create cross-compiled binaries. The terms used in this scenario are dynamically linked vs. statically linked binaries. When building a dynamically linked binary, the created executable will not pack every library it needs into itself but remembers the address spaces to call to when needing to make a system call to the operating system. The differences are shown in figure 10.3.
- Assistant
- Rust compiler uses operating system libraries during compilation, so each build is dependent on the underlying operating system it is running on.
- Rust supports the compilation for different environments, called targets.
- To check available targets, use the command
rustup target list. - To add an additional target to your toolchain via Rustup, use the command
rustup target install <target-name>. - To build your codebase for a specific environment, use the command
cargo build --release --target <target-name>. - Cross-compiled binaries can be either dynamically linked or statically linked.
- Dynamically linked binaries are smaller in size but rely on the exact operating system they are built on.
- Statically linked binaries include libraries within the binary itself, making them independent of the operating system.
- Rust offers a target for cross-compiled Linux binaries:
x86_64-unknown-linux-musl. - The musl project provides an implementation of the standard C library optimized for static linking.
- To build a statically linked binary, you need musl installed on your machine and sometimes specify the environment you are targeting through environment variables in your shell.