francoiscampbell / CLionArduinoPlugin

A plugin for CLion that enables arduino-cmake integration.
155 stars 42 forks source link

How to include headers #17

Closed peterkolski closed 8 years ago

peterkolski commented 8 years ago

How can I include the own libs of the Arduino SDK. Like eg. the Servo.h In the Arduino IDE the code below compiles fine, while in CLion there is the following error message:

undefined reference to `Servo::Servo()'

#include <Servo.h>

void setup() {
    Servo one;
}

void loop() {
}

Thank you very much

francoiscampbell commented 8 years ago

In your CmakeLists.txt file, add this below the project() line:

include_directories(<sketchbook location>/libraries/Servo)

where <sketchbook location> is the full path to your Arduino sketchbook folder.

peterkolski commented 8 years ago

If I copy now the Servo files from the SDK (mac) into the location of my CLion project, then the header should be found. But the .cpp files are in another folder, e.g. ${CMAKE_SOURCE_DIR}/Servo/avr Anyway, if I copy the files into the same folder, it still doesn't work:

project(${PROJECT_NAME})
include_directories( ${CMAKE_SOURCE_DIR}/Servo )

Does this mean, each standard Arduino Lib has to be copied into another directory? The Servo Lib is shown in CLion as "External Libraries" and can be included by code completion: #include <Servo/src/Servo.h> Still not compiling... Also the example not

francoiscampbell commented 8 years ago

Aah, seems the Servo library has a different folder structure than Arduino-cmake expects. add set(Servo_RECURSE True) right before generate_arduino_firmware(${CMAKE_PROJECT_NAME}) and it should work.

As a note, the required folder stucture is

ExampleLib/
  |-- ExampleLib.h
  |-- ExampleLib.cpp
  `-- OtherLibSource.cpp

but the Servo library has

Servo/
    |-- src/
        |-- avr
            |-- Servo.cpp
            |-- ServoTimers.h
        |-- sam
            |-- Servo.cpp
            |-- ServoTimers.h
        |-- Servo.h
peterkolski commented 8 years ago

That solved it! Merci!