Home Compile locally and run on raspberry pi via Clion
Post
Cancel

Compile locally and run on raspberry pi via Clion

Background

Compiling C/C++ code on the raspberry pi is very slow, I have some code I am writing that compiles in less than 5 seconds on the laptop but can take 30 minutes on the raspberry pi. This is pretty annoying and slows down testing of code tweaks.

I have found a way of automating this quite successfully via Clion (Commerical software from Jetbrains).

Raspberry pi tools

Firstly we need to get the raspberry pi compile toolchain.

https://github.com/raspberrypi/tools

Git clone this somewhere on your local filesystem.

Cmake

Clion integrates with CMake nicely and I have used it here for the cross compiling. I assume you already have a CMake project and have some CMakeLists files etc.

Create a new directory for the cmake toolchain file or put it in the root directory.

Call this file anything you like with a .cmake extension.  Here we call it:

toolchain-rasppi.cmake

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

# Specify the cross compiler
SET(CMAKE_C_COMPILER $ENV{HOME}/SoftwareDevelopment/Libaries/raspberrypi-toolchains/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/SoftwareDevelopment/Libaries/raspberrypi-toolchains/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)

# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/SoftwareDevelopment/Libaries/raspberrypi-toolchains/arm-bcm2708/arm-linux-gnueabihf)
#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
#SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
#SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")

# Search for programs only in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Notice how I have referenced the git cloned raspberry pi tool project in the above toolchain file.

Clion Settings

Now open the Clion settings page and go to the cmake profiles page. Add a new profile that uses the new toolchain via the cmake options.

-DCMAKE_TOOLCHAIN_FILE=rasp/toolchain-rasppi.cmake

The cmake profiles settings page within the program clion

Next add the raspberry pi as a new toolchain and call it something like "remote".

Go to Settings-Build,Execution, Deployment-Toolchains and add a remote host.

Clion Run/Debug Configurations

Now we have those two settings set we should be able to manipulate some run configurations. Unfortunately we can't tell Clion to build locally and run remotely but we can fudge it.

I am using GoogleTest frame work in my project so I will be using this and cmake run configs.

As you can see from the image above I have added another step in the before launch setting. Click the + symbol and add Run another configuration. From there you can also select which toolchain to run against. Select the remote one as this is the step we want to run on the raspberry pi.

Now open up the step that we added to the before launch setting of the other configuration. We just need to remove the build step from the before launch.

We do this because we don't want it trying to build on the raspberry pi itself. It should run what was uploaded to the pi via Clion's rsync.

Clion Edit upload settings

We now need to tweak the upload settings of Clion so that it changes the permissions to execute otherwise it will fail to run the executable on the raspberry pi.

Go to tools-deployment-options and set the preserve original file permissions to true. We can also explicitly change it to whatever we like.

Time to test

Select the run configuration that also calls the remote configuration (The first one we changed) from the drop down. Select the cmake profile that builds using the raspberry pi toolchain. It should look something like below.

Press the run button and it should work.

Annoyingly it will open another run tab for the first configuration. You can just close it.

If we switch to the first tab we can see it executed correctly.

 

Summary

So it seems to work although it feels a bit of a kludge. Hopefully Clion will keep on improving so we can select explicit build machine and separate deployment machines. For now this is better than waiting 30 minutes for build to run.

This post is licensed under CC BY 4.0 by the author.

If you have found this site useful, please consider buying me a coffee :)

Proud supporter of the Gnome Foundation

Become a Friend of GNOME

Contents

CherryTree custom codeblock background

weather Station in C++ on Raspberry Pi

Comments powered by Disqus.