【问题标题】:Using CLion to develop Arduino libraries使用 CLion 开发 Arduino 库
【发布时间】:2018-02-20 12:13:16
【问题描述】:

我目前正在使用 Kate 和 Arduino IDE 为 Arduinos 开发一个库(实际上很少有 Arduino 特定,但只需将示例从 A-IDE 上传到物理硬件很容易)。但是现在我已经到了 Kate 使用起来相当麻烦的地步,所以我尝试切换到 CLion,问题是我无法在其中进行 linting 和编译。我尝试使用Arduino plugin for CLion,它适用于小示例,但对我来说失败了,请记住,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)

“您为库 ./modules/lib3/lib3.h 调用了 ADD_LIBRARY,但没有任何源文件。”但是头文件指定了来源?

2:使用该 CMakefile CLion 无法在模块文件夹中找到标准库和其他库。例如,在查看 lib1.h 时,我看到这些错误(标记为红色):

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

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

Serialmemmovemalloc 等也是如此。

有什么想法可以解决这些问题并使用合适的 IDE 来开发我的项目吗?

【问题讨论】:

  • "You have called ADD_LIBRARY for library ./modules/lib3/lib3.h without any source files." but the header file specifies the source? - 技术上 - 是的,但通常 - 不是。 头文件指定库的接口。但是本身应该实现接口,也就是头文件中描述的函数。这就是为什么普通库需要至少有一个.c(或.cpp)文件的原因。如果库的接口不包含非inline函数,则该库称为interface-only,这种情况比较特殊。
  • @Tsyvarev 我指定源的意思是它包含#include "lib3.cpp"。所以它不仅仅是界面?
  • 包含.cpp 文件是一种非常奇怪的做法。通常只包含.h/.hpp
  • 修改后,我看到错误消息稍微说明了另一件事:add_library 的第一个参数是 target 名称,而不是文件。但剩下的参数应该是源文件。像这样的东西:add_library(lib1.h ./modules/lib1/lib1.cpp)。找不到头文件的问题通常由include_directoriestarget_include_directories 解决。顺便说一句,几乎任何 CMake 手册都描述了库的创建。我建议在继续之前阅读它。
  • 您使用的带有 Adruino 插件的项目间接引用了 that project 作为 Adruino 的 CMake 支持提供者。您在创建项目时是否尝试过遵循其文档?

标签: c cmake arduino clion


【解决方案1】:

由于MyLibrary.h 的设计令人怀疑,它不会修复您的整个项目。但试试这个库:

  • 将所有add_library(...) 替换为单个include_directories(${CMAKE_CURRENT_SOURCE_DIR}/modules)
  • 然后您可以重写模块的包含,例如:#include <lib1/lib1.n>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多