Native Compiling and Cross-Compiling BeagleBone Black Application[2]-Cross Compiling

In previous post we run our first application by using Native Compiling method on BeagleBone Black board. Then in this post, I will show you the remain method, Cross-Compiling.

1. What is Cross-Compiling?

Cross-Compiling uses a cross-compiler (cross-compiling toolchain) that works on Host PC, but their image can be run on other machine platform, in here is BeagleBone Black.

2. Cross-Compiling Installation

As you can see, there are 2 more things we need to prepare for cross-compiling.

a. Host PC installation

  • Virtualbox (Version 5.2.26) for Virtual Ubuntu machine installation.
  • Ubuntu version 12.04 installation on Virtualbox.

These installation steps can be found easily on Google. I will guide you how to do it in detail in other post.

b. Cross-Compiling toolchain installation

  • Assume that you succeed in Host PC installation, open your terminal by press at the same time “Ctrl+Alt+T”
  • Download the cross-compiling toolchain into your folder, I choose ~/work/toolchains
pvbinh@buildserver:~/work/toolchains$ wget -c
  • Extract file into current folder
pvbinh@buildserver:~/work/toolchains$ tar xf gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf.tar.xz
  • Update “PATH” variable in ~/.profile to help OS find toolchain binary
pvbinh@buildserver:~$ vi /home/pvbinh/.profile 
  • Inside the file, adding bellow line at end of file
# set PATH so it includes toolchain for BeagleBone Black
if [ -d "$HOME/work/toolchains/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin" ] ; then
  • Run source command to apply the modification
pvbinh@buildserver:~$ source ~/.profile 
  • Now check the toolchain is ready in your Host PC
pvbinh@buildserver:~$ arm-linux-gnueabihf-gcc  --version
arm-linux-gnueabihf-gcc (Linaro GCC 6.5-2018.12) 6.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

3. Cross-Compiling BeagleBone application

  • Create a new file for your application on Host PC vi hello_beagle_bone.c
pvbinh@buildserver:~/work/BeagleBone_Black/linux_programming_blog/01_cross_compile_application$ vi hello_beagle_bone.c
  • Add the content for your application
#include <stdio.h>

int main(void)
        printf("Hello Beagle Bone from Host PC\n");
        return 0;
  • Build the source by using cross-compiler arm-linux-gnueabihf-gcc -o hello_beagle_bone hello_beagle_bone.c
pvbinh@buildserver:~/work/BeagleBone_Black/linux_programming_blog/01_cross_compile_application$ arm-linux-gnueabihf-gcc -o hello_beagle_bone hello_beagle_bone.c
  • You should see hello_beagle_bone output image, and it cannot be run in Host PC
bash: ./hello_beagle_bone: cannot execute binary file
  • Let check whether it can run on BeagleBone Black or not

4. Run binary on BeagleBone Black

  • First you need to install ssh, then you can use scp to transfer file from Host PC to BBB
sudo apt-get install openssh-server openssh-client
  • Copy file content to your BeagleBone Black, this command is run on host PC.
sudo scp hello_beagle_bone debian@
  • In here:
    • debian: BBB user name
    • BBB ip address
    • /home/debian: folder to save send file
  • Please note fill your Host PC user password for sudo and BBB user password for scp command:
sudo scp hello_beagle_bone debian@
[sudo] password for pvbinh: 
debian@'s password: 
hello_beagle_bone                      100% 9572     9.4KB/s   00:00
  • Now your hello_beagle_bone is transferred, check it on your BBB
debian@beaglebone:~$ ls -l
total 36
drwxr-xr-x 2 debian debian 4096 Oct  7  2018 bin
drwxr-xr-x 2 debian debian 4096 Oct  7  2018 Desktop
-rwxr-xr-x 1 debian debian 8344 Jan 20 06:10 hello
-rwxr-xr-x 1 debian debian 9572 Jan 20 11:22 hello_beagle_bone
-rw-r--r-- 1 debian debian   97 Jan 20 06:21 hello.c
  • Run your application
debian@beaglebone:~$ ./hello_beagle_bone
Hello Beagle Bone from Host PC
  • We succeed cross-compiling an application for BBB

5. Advantage and Disadvantage

You can see the disadvantage of cross-compiling in installation and development process. However it is really helpful when working with real project:

  • Save your build time because your Host PC is really powerful
  • Make your implementation become more independent, you don’t need to access BBB for writing your code, then you can use your powerful editor instead of Vi
  • You can save and manage your source code in your Host PC, more safety compare to save code in you BBB and use Native Compiling
  • Cross-Compiling only take you one time to setup, from the second time, everything becomes very convenient

6. What should we do next?

We finished the first step on creating working environment with BBB. To go deeper on Linux Programming, we should understand the overview about Linux Software Architecture. Then I will show you how to work with each of them one by one.

Hope to see you soon.

One thought on “Native Compiling and Cross-Compiling BeagleBone Black Application[2]-Cross Compiling”

Leave a Reply

Your email address will not be published. Required fields are marked *