4
votes

I'm currently using Kate and Arduino IDE to develop a library for Arduinos (actually rather little is Arduino specific but it's easy to just upload examples from A-IDE to physical hardware). But now I've reached the point Kate is rather cumbersome to use so I tried to switch to CLion, the issue is that I can't get linting and compilation working in it. I tried using the Arduino plugin for CLion, it kind-of works for small examples, but fails for me, please keep in mind that Arduino IDE has no problem compiling my code and uploading it.

This is my CMakeFiles.txt:

cmake_minimum_required(VERSION 2.8.4)
set(ARDUINO_SDK_PATH ${HOME}/arduino-1.8.2/)
set(PROJECT_NAME MyLibrary)
project(${PROJECT_NAME})
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)

add_library(./modules/lib1/lib1.h)
add_library(./modules/lib2/lib2.h)
add_library(./modules/lib3/lib3.h)
add_library(./modules/lib4/lib4.h)

set(EXAMPLE2_SKETCH ./examples/example2/example2.ino)
set(EXAMPLE1_SKETCH ./examples/example1/example1.ino)

generate_arduino_firmware(EXAMPLE2_SKETCH)
generate_arduino_firmware(EXAMPLE1_SKETCH)

Folder structure:

MyLibrary
├── drivers
│   └── HWLIB
│       ├── HWLIB.cpp
│       └── HWLIB.h
├── examples
│   ├── example1
│   │   └── example1.ino
│   └── example2
│       └── example2.ino
├── modules
│   ├── lib1
│   │   ├── lib1.cpp
│   │   └── lib1.h
│   ├── lib2
│   │   ├── lib2.cpp
│   │   └── lib2.h
│   ├── lib3
│   │   ├── lib3.cpp
│   │   └── lib3.h
│   └── lib4
│       ├── lib4.cpp
│       └── lib4.h
└── MyLibrary.h

1: After trying to refresh the cmakefile then CMake complains about almost every line and it doesn't make any sense:

CMake Warning (dev) at CMakeLists.txt:7 (add_library):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "./modules/lib1/lib1.h" is reserved or not valid
  for certain CMake features, such as generator expressions, and may result
  in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

You have called ADD_LIBRARY for library ./modules/lib1/lib1.h without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Warning (dev) at CMakeLists.txt:8 (add_library):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "./modules/lib2/lib2.h" is reserved or
  not valid for certain CMake features, such as generator expressions, and
  may result in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

You have called ADD_LIBRARY for library ./modules/lib2/lib2.h without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Warning (dev) at CMakeLists.txt:9 (add_library):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "./modules/lib3/lib3.h" is reserved or not valid
  for certain CMake features, such as generator expressions, and may result
  in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

You have called ADD_LIBRARY for library ./modules/lib3/lib3.h without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Warning (dev) at CMakeLists.txt:10 (add_library):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "./modules/lib4/lib4.h" is reserved or not
  valid for certain CMake features, such as generator expressions, and may
  result in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

You have called ADD_LIBRARY for library ./modules/lib4/TooSigning.h without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Generating NODE_SKETCH
CMake Error at cmake/Platform/Arduino.cmake:2130 (message):
  ALL_SRCS not set: must define SRCS or SKETCH for target NODE_SKETCH
Call Stack (most recent call first):
  cmake/Platform/Arduino.cmake:498 (required_variables)
  CMakeLists.txt:22 (generate_arduino_firmware)

"You have called ADD_LIBRARY for library ./modules/lib3/lib3.h without any source files." but the header file specifies the source?

2: With that CMakefile CLion fails to find both standard libraries and other libraries in the modules folder. For example when viewing lib1.h I see these errors (marked red):

#include "modules/lib2/lib2.h"
          ^ Cannot find 'modules'

and

uint8_t variable = 0;
^ Can't resolve type 'uint8_t'

Same goes for Serial, memmove, malloc and etc.

Any ideas how I could fix these issues and use a proper IDE to develop my project?

1
"You have called ADD_LIBRARY for library ./modules/lib3/lib3.h without any source files." but the header file specifies the source? - Technically - yes, but usually - no. The header file specifies an interface of the library. But the library itself should implement the interface, that is functions described in the header file. That is why normal library is required to have at least one .c (or .cpp) file. If a library's interface contains no non-inline function, the library is known as interface-only, this case is special.Tsyvarev
@Tsyvarev What I meant by specifying the source is that it contains #include "lib3.cpp". So it's not interface only?Avamander
Including .cpp files is a very weird practice. Normally, only .h/.hpp are included.Tsyvarev
After revising I see the error message says slightly another thing: the first parameter to add_library is a target name, not a file. But remained parameters should be source files. Something like this: add_library(lib1.h ./modules/lib1/lib1.cpp). A problem with a header file which cannot be found is resolved by usual include_directories or target_include_directories. BTW, creation of the libraries is described in almost any CMake manual. I suggest to read it before proceed further.Tsyvarev
The project with Adruino plugin you use indirectly refers to that project as provider of CMake support for Adruino. Have you tried to follow its documentation when create you project?Tsyvarev

1 Answers

2
votes

It won't fix your whole project, due to doubtful design with MyLibrary.h. But try this for libraries:

  • Replace all of add_library(...) with just single include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules)
  • Then you can rewrite includes for modules like: #include <lib1/lib1.n>