Skip to content

Using CMake to create projects

steamypotato edited this page Jan 23, 2021 · 1 revision

IN PROGESS WILL BE DONE BY TOMMOROW NIGHT

Creating a .ino project

By default, CMake will detect any .ino files and inject a CMakeLists.txt file in the directory where the .ino resides. Thus a file with #include <Arduino.h> and a setup and loop function will compile successfully.

Adding libraries

I will demonstrate an example using the Servo library. To do this you need to be the CMakeLists.txt file in robot/rover. Since I will use an example, so to figure out where in the code you need to write this stuff just Ctrl-F to find the example and just append it to keep things clean.

Set the path to the sources

Use the set function, where the first argument is the reference to the path, and the second argument is the actual path on your computer, in this case ${TEENSY_LIB} is a path where all the basic libraries are stored. set(SERVO_PATH ${TEENSY_LIB}/Servo)

Create the library from the sources

createCppLibrary(${SERVO_PATH}/Servo.cpp Servo "") The first argument are the sources for the library. If there are multiple of them, use this format "path1/foo.cpp;path2/bar.cpp" for the first argument. The second argument is the the name of the created library (it will stored as libname.a when created, but you reference it by just name). The last argument is in the case where the source needs additional include directories. For example, Servo.h is stored alongside Servo.cpp, so there are no issues, but if Servo.h was stored in Servo/include you would set the third arg to ${SERVO_PATH}/include.

Give a reference to your new library to your project

Still in the same CMakeLists.txt file, go down to the for each loop. Follow this example if(${name} STREQUAL "Arm") set(PROJECT_LIBRARIES Servo)

Instead of Arm, use the name of your .ino file. Instead of Servo, use the name of your library, the same name that was the second argument in the function in the previous step.

Creating a .cpp file

If you are just adding functionality to a .ino project, the system will detect your file and automatically compile/link it to the .ino project, so there is nothing to be done on your side.

Create a library

A good example of this one is the comms library. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Clone this wiki locally