5

HAWK-Rust Series: Cross compiling Rust

 2 years ago
source link: https://blog.knoldus.com/hawk-rust-series-cross-compiling-rust/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading Time: 3 minutes
Hawk Logo

Internet of Things has already taken the technology world by a storm and is continuing to do so. While learning about IoT, its uses and all that stuff, a desire to implement that in Hawk project arose. So, we’ll try to cross compile the Hawk project that is purely Rust based

Cross compiling

Raspberry Pi is the most known IoT device to the beginners. Some more complex IoT devices include Google Home, Amazon Alexa and the new Nescafe E for making your coffee experience smarter.

Now coming back to the Rust, let’s see(actually read!) how can we make our Rust code to work with an IoT device. In this blog, I will be sharing about Raspberry Pi-Rust integration for a Linux platform for Hawk.

Image result for raspberry piRaspberry Pi

Step 1: Installing the toolchain

Cross-compiling the rust code into the Raspberry Pi requires a toolchain. The toolchain can be downloaded by typing the following command on your Linux terminal.

curl https://sh.rustup.rs -sSf | sh

This command downloads a shell script file from the sh.rustup.rs and the file is run using the pipe symbol ( | ) which is used to install the rust toolchain.

If Rust is already installed on your system, you would get the following error:

info: downloading installer
error: it looks like you have an existing installation of Rust at:
error: /usr/bin
error: rustup cannot be installed alongside Rust. Please uninstall first
error: if this is what you want, restart the installation with `-y'
error: cannot install while Rust is installed

If this error occurs, run this command first and then the previous command.

sudo apt remove rustc

And finally the main toolchain command

rustup target add armv7-unknown-linux-gnueabihf

This will download a 53.0 MB file to your system which is the toolchain for cross compilation and you will get the following output:

info: downloading component 'rust-std' for 'armv7-unknown-linux-gnueabihf'
 53.0 MiB /  53.0 MiB (100 %) 567.3 KiB/s ETA:   0 s                
info: installing component 'rust-std' for 'armv7-unknown-linux-gnueabihf'

Step 2: Getting the C compiler

In order to run you code on Raspberry Pi, you need a C compiler to compile the program. You can install the C Compiler using the command:

sudo apt-get install gcc-4.7-multilib-arm-linux-gnueabihf

Step 3: Adding the right Linker

In this step, we would add a linker for the ARM architecture. For that you need to go to Home directory and type the following command.

cd .cargo
touch config
gedit config

This will make a new file named config in the .cargo directory inside the Home directory. Now, add the target to the file by concatenating the file with this:

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc-4.7"

Step 4: Editing Cargo.toml

Cargo.toml file needs to be added with openssl dependency with a vendored feature and will look like this:

[package]
name = "factorial"
version = "0.1.0"
authors = ["mudit <[email protected]>"]
edition = "2018"
[dependencies]
openssl = { version = "0.10", features = ["vendored"] } 

The Final Step: Building the project for Raspberry Pi

The project can be build using –target option with the cargo build command.

cargo build --target=armv7-unknown-linux-gnueabihf

You can check if the project is compiled according to the Raspberry Pi Architecture by checking this directory:

target/armv7-unknown-linux-gnueabihf/debug/factorial

Now you are ready to run this project on Raspberry Pi.

Thank you for coming this far in the blog. If you have any suggestions for the blog/project, you can surely give your comments.

For complete information about Project Hawk, you can check this Blog.

For more reference and contributing to our open source project Hawk, you can visit here.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK