Cmake for Arduino and Clion

For a project I just started working on I wanted to use Clion with Arduino, since there is no Arduino plugin or support for Clion yet I had to figure out a way to do this with Cmake. In this post I'll try to explain what to do.

Required resources

Ofcourse the Arduino SDK needs to be installed and a Cmake library is required. For this I found Arduino Cmake which can be found at

The project

So lets get started. First create a project directory, mkdir ~/workspace/ArduinoCmake and move into the directory. cd ~/workspace/ArduinoCmake. Let's get the toolchain by going a git clone This will get the CmakeFiles needed to do arduino related tasks like uploading it to the Arduino. Next we create a source file: nano main.cpp in this file we put a simple demo:

#include <HardwareSerial.h>

#include <Arduino.h>

int incomingByte = 0;    // for incoming serial data

void setup() {

    Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps


void loop() {
    // send data only when you receive data:

    if (Serial.available() > 0) {

        // read the incoming byte:

        incomingByte =;

        // say what you got:




This demo will do a serial echo so we can send data to the Arduino which it will echo back. Next we create the CmakeLists.txt: nano CMakeLists.txt and in this file put the following: cmake_minimum_required(VERSION 2.8.4)

set(CMAKE_TOOLCHAIN_FILE arduino-cmake/cmake/ArduinoToolchain.cmake)






    SRCS main.cpp


I will highlight the important lines: The set(CMAKE_TOOLCHAIN_FILE line tells Cmake where the Arduino toolchain is located. This line must be before the project line. The next lines, ARDUINO_DEFAULTs, tell the Arduino toolchain which board is used and on which port it is located. In the generate_arduino_firmware command we tell the toolchain which sourcfiles to use.

Building the project

To build the project create a build directory: mkdir build and move into it: cd build. Next use cmake to build the project: cmake ../ and this creates make file. To build the project use make and to upload the project use make upload