使用CLion开发Arduino库

4
我目前使用Kate和Arduino IDE开发Arduinos库(实际上很少有Arduino特定的内容,但是从A-IDE上传示例到物理硬件很容易)。但现在我发现使用Kate相当麻烦,所以我尝试切换到CLion,但问题是我无法在其中启用linting和编译。我尝试使用CLion的Arduino插件,它对于小的示例代码有点作用,但是对我来说不起作用,请注意,Arduino IDE没有问题编译我的代码并将其上传。
这是我的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)

文件夹结构:

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:在尝试刷新cmakefile之后,CMake会抱怨几乎每一行代码,而且这没有任何意义。

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)

您已经调用了ADD_LIBRARY命令去创建库文件"./modules/lib3/lib3.h",但是没有指定源文件?

2: 在CMakefile中这样做会导致CLion找不到标准库和其他在modules文件夹中的库。例如,在查看lib1.h时,我看到了以下错误(用红色标记):

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

并且

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

同样适用于Serialmemmovemalloc等。
你有什么想法可以解决这些问题并使用合适的IDE来开发项目吗?

1
“您在没有任何源文件的情况下调用了ADD_LIBRARY来操作./modules/lib3/lib3.h库。 ”但头文件指定源代码?技术上是的,但通常不是这样。“头文件”指定库的“接口”。但是“库”本身应该“实现接口”,即头文件中描述的函数。这就是为什么普通库需要至少一个“.c”(或“.cpp”)文件的原因。如果库的接口不包含非inline函数,则该库被称为“仅接口”,这种情况很特殊。 - Tsyvarev
你展示的目录结构只包含 .c.h 文件。如果这些源文件是用于库的,那么这些库是否已经被构建了呢?也就是说,是否已经有了 .a.lib 文件,或者 .dll 文件? - ryyker
@ryyker AVR库的源文件,是的。没有.a.lib,我可以假设那些预编译的库文件只有在处理动态链接库时才是必需的,而在静态链接时像Arduino IDE/avr-gcc一样则不需要吗? - Avamander
1
修改后,我看到错误信息说的是另一件事:add_library 的第一个参数是目标名称,而不是文件。但是其余的参数应该是源文件。像这样:add_library(lib1.h ./modules/lib1/lib1.cpp)。无法找到头文件的问题可以通过常规的include_directoriestarget_include_directories来解决。顺便说一下,在几乎任何CMake手册中都有关于库的创建描述。我建议在继续之前先阅读它。 - Tsyvarev
1
你使用的带有Adruino插件的项目间接地参考了该项目作为Adruino的CMake支持提供者。在创建你的项目时,你尝试过遵循它的文档吗? - Tsyvarev
显示剩余6条评论
1个回答

2

由于MyLibrary.h的设计存在疑问,因此它不会修复您的整个项目。但是对于库,请尝试以下操作:

  • 将所有add_library(...)替换为单个include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules)
  • 然后,您可以重写模块的包含方式,例如:#include <lib1/lib1.n>
"Original Answer"翻译成"最初的回答"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接