Wiki

Clone wiki

roseline / Tutorial 3 - Cross-compiling applications for the platform

Overview

Compiling C or C++ applications on the BeagleBoard Black itself can be extremely tedious, as the platform has limited processing and memory resources. Thankfully, setting up a cross-compiler in Ubuntu Linux is relatively easy. The goal of this tutorial is to install a cross compiler and build a test application for the BeagleBone Black.

First and foremost, install essential build tools, cmake and the C/C++ cross-compilers:

sudo apt-get install build-essential cmake gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf

Here's a CMakeLists.txt build script to cross-compile a simple "hello world" application, which is linked against the version of boost running on the platform. The only tricky thing to note is that you will need a local copy of the root file system used by the BeagleBone Black at some location /path/to/rootfs.

# Minimum version of CMake required to build this file
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(test)

# Cross-compile properties
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR arm) 
SET(CMAKE_SYSTEM_VERSION 1)

# Specify the cross compiler
SET(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
SET(CMAKE_FIND_ROOT_PATH /path/to/rootfs)

# Search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Convenience
SET(CMAKE_C_FLAGS "-std=c99") 

# Preprocessor directive required to link boost trivial logging
FIND_PACKAGE(Boost 1.55 REQUIRED COMPONENTS thread system program_options)

# Location of header files for the entire project
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR} "/path/to/usr/include/arm-linux-gnueabihf")

# Location of boost libraries
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS} "/path/to/rootfs/usr/arm-linux-gnueabihf/lib")

# Test application
add_executable(test test.cpp)
target_link_libraries(test)

And, here is a simple test application, which should be written to test.cpp.

#include <iostream>
int main(int argc, char *argv[])
{
    std::cout <<  "hello world" << std::endl;
    return 0;
}

I always prefer keeping my build products separate from my source. So, I make a build directory, create the make files and build using four threads (-j4) in the following way:

mkdir build
cd build
cmake ..
make -j4 

You can then transfer test to the BeagleBone Black and run it.

Updated