Skip to content

Linux X86 ARM Cross Compilation

Mael Rouxel-Labbé edited this page Mar 28, 2024 · 1 revision

Table of Contents

Requirements

Getting the Compilation tools

The first step for cross-compiling a library to arm is to get the right tools for it:

> sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf

Those are the C and C++ compilers that will take care of the cross-compilation to a 32bit arm platform.

For 64bit:

> sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

In the rest of this walkthrough, we will use the 32bit tools. For 64bit do the exact same thing but replacing arm-linux-gnueabihf with aarch64-linux-gnu.

Building GMP

In the GMP sources directory, do the following:

> CC=/usr/bin/arm-linux-gnueabihf-gcc ./configure --prefix=${GMP_PATH} --enable-shared --host=${TARGET_TRIPLET}

> make
> make install

Building MPFR

This is very similar to building GMP. In the MPFR sources directory, do the following:

> CC=/usr/bin/arm-linux-gnueabihf-gcc ./configure --prefix=${MPFR_PATH} --enable-shared --host=${TARGET_TRIPLET} --with-gmp=${GMP_PATH}

> make
> make install

Building Boost (Optional)

You only need this if you intend to use CGAL_Core and/or Classification, otherwise the header only version of boost is sufficient. For Classification, you will need the zlib sources. You can easily cross-compile it using the toolchain seen below. Get the sources and go in the sources directory. In here, type those:

  • ./bootstrap.sh
  • Edit project-config.jam and replace using gcc by: using gcc : arm : arm-linux-gnueabihf-g++ ;
  • ./bjam install toolset=gcc-arm --prefix=${Boost_DIR} -s ZLIB_SOURCE=/path/to/zlib/ -s ZLIB_INCLUDE=/path/to/zlib/ -s ZLIB_LIBPATH=/path/to/zlib/build/dir
    If you don't want Classification, forget all about ZLIB.

Configuring CGAL

Now that we have the dependencies built, we can configure CGAL. First we will create a toolchain.cmake file, containing the paths needed by CMake. Create this file at ${TOOLCHAIN_FILE_PATH}:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(GMP_INCLUDE_DIR ${GMP_PATH}/include)
set(GMP_LIBRARIES ${GMP_PATH}/lib/libgmp.so)

set(MPFR_INCLUDE_DIR ${MPFR_PATH}/include)
set(MPFR_LIBRARIES ${MPFR_PATH}/lib/libmpfr.so)

set(Boost_INCLUDE_DIR ${BOOST_PATH})

Once we have this, we call cmake:

> cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE_PATH} -DWITH_CGAL_Core=FALSE -DCMAKE_CXX_FLAGS=-std=c++11 -DCGAL_test_cpp_version_RUN_RES=0 -DCGAL_test_cpp_version_RUN_RES__TRYRUN_OUTPUT=201103L -DWITH_CGAL_Qt5=FALSE /path/to/cgal/sources

CGAL is now ready to be used with an arm application.

Notes:

  • If you omit -DCGAL_test_cpp_version_RUN_RES__TRYRUN_OUTPUT=201103L, you will have to run cmake twice..
  • The options -DCMAKE_CXX_FLAGS=-std=c++11 -DCGAL_test_cpp_version_RUN_RES=0 -DCGAL_test_cpp_version_RUN_RES__TRYRUN_OUTPUT=201103L allow to use the C++11 standard. When CGAL is compiled with a C++11 compiler, the library Boost.Thread is no longer required, and Boost libraries can be used header-only. There is one exception: CGAL_Core still requires Boost.Thread. That is why we use the cmake option -DWITH_CGAL_Core=FALSE, to disable CGAL_Core. If you need CGAL_Core, then you will have to cross-compile Boost, and then set the appropriate CMake variables. Some packages have more dependencies. If you wish to use them, you will have to cross-compile them too.

Setting up a testsuite

This section explains how to set up a testsuite that will cross-compile the tests on an x86 machine and run them on an arm machine (a Raspberry Pi 3B running on Raspbian) via ssh.

Setup sshd

First of all, setup ssh on the machine. From the official RPi manual, you can do it graphically :

  • Launch Raspberry Pi Configuration from the Preferences menu
  • Navigate to the Interfaces tab
  • Select Enabled next to SSH
  • Click OK

or in a shell:

sudo systemctl enable ssh
sudo systemctl start ssh

From now on, everything should be done on the x86 machine. To set it up:

cd ~/.ssh
ssh-keygen -o
ssh-copy-id -i [your generated key] [user@hostname]

You can test it by running ssh [user@hostname] to see if you can connect.

Install CGAL dependencies

sudo apt-get install cmake libboost1.62-all-dev libgmp-dev libmpfr-dev

The Infrastructure

From now on, you have to setup a CGAL_ROOT directory as explained in Setup a testsuite with ctest.

Clone this wiki locally