• Home
  • Cross-Compiling Programs

Cross-Compiling Programs

The second bullet of this week’s 2-Bullet Tuesday! Check out the first bullet here. Subscribe to receive the newsletter on the 2-Bullet Tuesday page!

Omega Tip

We’re continuing our series on cross-compiling programs for the Omega2. Last week, we covered we covered the pre-requisites: setting up the build system by pulling and compiling the Omega2-Source Docker Image . This week, we’ll be showing you how to use the compiled build system to cross-compile your own programs! Keep in mind, the following procedure has to be done in the docker container

Let’s start with a simple C program, very similar to the program we compiled on the Omega.To create your first program, simply create a file called hello.c and populate it with the following code:

#include‹stdio.h›

int main()
{
        printf("Hello World\n");
        printf("We are running a cross-compiled C program on the Omega2!\n");
        return(0);
}

We now need to cross-compile this code into an executable binary for the Omega. For regular compilation, we would use a command like the following: gcc hello.c -o hello

To cross-compile for the Omega, we’ll need to use the Omega-specific gcc compiler generated by the build system. This compiler can be found at: /root/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-gcc Note how it’s called mipsel-openwrt-linux-gcc, the very name describes for which system it compiles code.

To actually compile:

/root/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-gcc hello.c -o hello

To confirm the output of the compilation is indeed cross-compiled, we can use the handy file command:

root@6cef4c9123cb:~/source/c-cross-compile-example# file hello
hello: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld-musl-mipsel-sf.so.1, not stripped

As you can see, it’s an executable for the MIPS platform of the Omega.

Now we’ll need to get the compiled executable over to your Omega. We found the online transfer.sh platform great for this purpose. We’ll first upload the binary from the Docker image, and then download it on an Omega.

The uploading process will consist of the following command:

curl --upload-file ./hello https://transfer.sh/hello

It will generate a web link from which you can download your binary executable file. It will look something like this: https://transfer.sh/bJoES/hello. And your file is online and ready to be downloaded!

root@6cef4c9123cb:~/source# curl --upload-file ./hello https://transfer.sh/hello
https://transfer.sh/b6wtw/helloroot@6cef4c9123cb:~/source#

To download your program onto your Omega, issue the following command from the Omega terminal:

wget https://transfer.sh/bJoES/hello

The last step is to make your hello file executable. Run chmod +x helloand now you are all set! The binary executable file is now in your directory and you can execute it on the Omega. Just issue the command:

./hello

and you will get the following output:

Hello World
We are running a cross-compiled C program on the Omega2!

And there you go! You’ve officially cross compiled a program with the build system and then executed it on your Omega. Next week, we’ll cover how to cross-compile a more involved program that makes use a library!

 

Thanks for reading & have a great week!

Have you seen the Omega2S, the smaller and surface-mount version of the Omega2 for high volume commercial and industrial OEMs? See our Omega2S page for more details!

Let us know what kind of stuff you would like to see featured on 2-Bullet Tuesday! Send a tweet to @OnionIoT with your suggestions!

P.S. Hey, if you are in Denver this weekend, come stop by and see us at

P.P.S. We’re beyond excited to announce that the Omega2 family is now available on SparkFun!



facebook twitter google-plus instagram youtube

© 2017 Onion Corporation

 

Share

Pavel Metrokhin