Giter Club home page Giter Club logo

rust_cross_compilation's Introduction

Done by Lincoln Rust language

💻 Installation

# Clone this repo on your computer 
$ git clone https://github.com/LOCNNIL/rust_cross_compilation.git

If you don't have rust on your computer install it acessing Rust official site and follow the instructions.

After rust proced runing:

# Listing all arch. suported by rust:
$ rustc --print target-list

OBS: see platform suport of Rust documentation for more details

Then finding your target architecture name you can add:

# Find your architecture to cross-compile and run:
# rustup target add <your-arch>

# For my armv7l embedded computer
$ rustup target add armv7-unknown-linux-gnueabi 

# For my Raspberry Pi Zero W
$ rustup target add arm-unknown-linux-gnueabihf

It's necessary too point to rust where is the linker for this architecture, so let's build a folder with the configuration options inside the project root.

$ mkdir .cargo
$ cd .cargo
$ touch config.toml

OBS: If you want made this configs definitive you can modify directly the ~/.cargo/config file


📎 Specify the Linker

For my armv7l embedded computer:

It's necessary fist too have the gcc (or clang) compiler that comes with the linker, if you don't have it's necessary to install:

# In my case for the arch armv7l:
$ sudo apt install gcc-arm-linux-gnueabi

Then inside the config.toml we put what is the gcc (or clang) linker to be used pointing by the binarie. we point:

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

(You can either specify the whole path too, in my case it would be /usr/bin/arm-linux-gnueabi-gcc )

For the Raspberry Pi Zero W:

Clone the raspberrypi/tools repo into a directory named rpi_tools

$ git clone https://github.com/raspberrypi/tools $HOME/rpi_tools

Then we point to the linker in the config.toml file:

[target.arm-unknown-linux-gnueabihf]
linker = "~/rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"

🔗 Static linking

Depending of which type of embedded you are handling it may can be missing some dependencies. One way to avoid that is using static linking. This feat is optional but depending of your CoM could be interesting. With staticaly linkage, it's possible insert all dependencies of your application inside the binary. It can be possible adding this line:

rustflags = ["-C", "target-feature=+crt-static"]

So for the final config file we have:

# target config for Embedded Computer with armv7l arch
[target.armv7-unknown-linux-gnueabi]
linker = "arm-linux-gnueabi-gcc"
rustflags = ["-C", "target-feature=+crt-static"]

# target config for Raspberry Pi Zero W
[target.arm-unknown-linux-gnueabihf]
linker = "/home/lincoln/rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"
rustflags = ["-C", "target-feature=+crt-static"]

🏁 Building and Running

# The building using cargo
# cargo build --target <your-arch>

# For the armv7l CoM
$ cargo build --target armv7-unknown-linux-gnueabi

# For the Raspberry Pi Zero W
$ cargo build --target arm-unknown-linux-gnueabihf

# Or if you already config the arch in .cargo/config.toml
$ cargo build

Now you just need to upload the binary to your target device using ADB, SSH, FTP server or any other way you prefer.

Runing the for the native arch (my x86_64 computer)

native-compilation

Runing for my armv7l CoM:

cross-mdm9607

Runing in Raspberry Pi Zero W (armv6l):

cross-raspizerow

rust_cross_compilation's People

Contributors

locnnil avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.