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

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.

The cmake profiles settings page within the program clion
Cmake profiles in clion settings

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.

The clion run configs page

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.

This tab can be ignored

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

Google test ran correctly on the raspberry pi.

 

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.

Please share 🙂
error