Preface & Content
The rust compiler supports various targets with a few simple installation steps without the need to write specialized code. The following article will guide you throught the setup of a fully functional toolchain to write cross-compiled rust applications which will run on a large number of linux distributions.
Installation of the Rust toolchain
Download and install the rust toolchain from https://www.rustup.rs/. See other installation methods if you want to download the installer for other platforms. The installation of precompiled libraries is rather easy and can be achieved by the following commands. If you have already installed a default toolchain the targets can be added by executing only the last three commands.
The downloaded libraries will be placed under the rust installation folder where DEFAULT_TOOLCHAIN_NAME ist the name of the selected toolchain and TARGET_NAME is one of the installed targets.
For a fully functional cross-compilation target you have to specify a pre-installed linker. This can be done by downloading / installing a working GCC toolchain to cross-compile onto the target platform. See build a linux cross-compile toolchain on cygwin64 / windows for a step by step manual of how to build a toolchain capable of linking linux i686 / x86_64 programs on Windows / cygwin64.
To specify the linker name for rust we have to create a config file in a subdirectory named .cargo under our project directory. For each used target (see rustup target list) we have to define the used linker executable like seen below.
By specifying the --target switch the executable can now be compiled for the target system.
cargo build --target=i686-unknown-linux-gnuIf C code needs to be compiled by one or more of the used packages the CC environment variable will be used to call the compiler.
export CC=i686-unknown-linux-gnu-gcc cargo build --target=i686-unknown-linux-gnu
Clone from https://github.com/TobiasFaller/rust-test-application.git or visit https://github.com/TobiasFaller/rust-test-application.
Below is a small program to test the functionality of the Rust toolchain. Since a simple print("Hello World") is way too boring this application extracts a string from the embedded zip file of the source folder. This demonstrates the use of compiled native packages working together with the rust libraries.
The output on my native Windows system is the follwing:
$ cargo build Compiling winapi v0.2.8 Compiling podio v0.1.6 Compiling cc v1.0.4 Compiling winapi v0.3.4 Compiling adler32 v1.0.2 Compiling winapi-build v0.1.1 Compiling build_const v0.2.0 Compiling winapi-x86_64-pc-windows-gnu v0.4.0 Compiling libc v0.2.36 Compiling kernel32-sys v0.2.2 Compiling crc v1.7.0 Compiling miniz_oxide v0.1.2 Compiling bzip2-sys v0.1.6 Compiling miniz_oxide_c_api v0.1.2 Compiling flate2 v1.0.1 Compiling time v0.1.39 Compiling bzip2 v0.3.2 Compiling msdos_time v0.1.5 Compiling zip v0.3.0 Compiling cross_compile_example v0.1.0 (file:///.../Blog) Finished dev [unoptimized + debuginfo] target(s) in 28.3 secs $ ./target/debug/cross_compile_example.exe Hello cross-compile world!
While the cross-compilation produces the following output:
$ export CC=x86_64-unknown-linux-gnu-gcc $ cargo build --target=x86_64-unknown-linux-gnu Compiling adler32 v1.0.2 Compiling libc v0.2.36 Compiling podio v0.1.6 Compiling crc v1.7.0 Compiling miniz_oxide v0.1.2 Compiling time v0.1.39 Compiling bzip2-sys v0.1.6 Compiling bzip2 v0.3.2 Compiling msdos_time v0.1.5 Compiling miniz_oxide_c_api v0.1.2 Compiling flate2 v1.0.1 Compiling zip v0.3.0 Compiling cross_compile_example v0.1.0 (file:///.../Blog) Finished dev [unoptimized + debuginfo] target(s) in 5.29 secs $ x86_64-unknown-linux-gnu-readelf ./target/x86_64-unknown-linux-gnu/debug/cross_compile_example -h ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0xb220 Start of program headers: 64 (bytes into file) Start of section headers: 6816344 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 10 Size of section headers: 64 (bytes) Number of section headers: 46 Section header string table index: 45 Run on VM (Ubuntu / Debian Linux): administrator@administrator-VirtualBox:/media/sf_Shared$ ./cross_compile_example Hello cross-compile world!
See https://www.rust-lang.org/en-US/ for the applying licenses regarding the used software.
Regarding the used scripts / code on this page:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/