-
Notifications
You must be signed in to change notification settings - Fork 16
Using CMake to create projects
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.
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.
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)
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
.
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.
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.
A good example of this one is the comms library. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa