-1

我是 C、C++ 和 Cmake 的初学者。

我从一个项目开始,该项目允许我将数据发送到用 C 编写的 Google Cloud IoT Core:https ://github.com/espressif/esp-google-iot/blob/master/examples/smart_outlet/README .md

如您所见,我有一块带有 ESP32 芯片的电路板。

我买了一个我想使用的湿度传感器,并且库是用 C++ 编写的,所以将它们添加到 C 项目中非常困难。( https://github.com/adafruit/DHT-sensor-library ) ( https://github.com/adafruit/Adafruit_Sensor )

我在我的 CMakeLists.txt 中导入这样的库:

cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

# This is some google iot librares that are being imported in the project
set (EXTRA_COMPONENT_DIRS "../..")


# Libraries in cpp that are not being compiled correctly
INCLUDE_DIRECTORIES(lib/Adafruit_Sensor)
LINK_DIRECTORIES(lib/Adafruit_Sensor)
INCLUDE_DIRECTORIES(lib/DHT-sensor-library)
LINK_DIRECTORIES(lib/DHT-sensor-library)

project(smart_outlet)

但是在编译 c++ 库时我收到一条错误消息。

../lib/DHT-sensor-library/DHT_U.h:45:1: error: unknown type name 'class'

我认为抛出错误是因为 c 不支持 c++ 类。

这是我向我抛出错误的 c++ .h 文件:

#ifndef DHT_U_H
#define DHT_U_H

#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHT_SENSOR_VERSION 1 /**< Sensor Version */

/*!
 *  @brief  Class that stores state and functions for interacting with
 * DHT_Unified.
 */
class DHT_Unified {
public:
  DHT_Unified(uint8_t pin, uint8_t type, uint8_t count = 6,
              int32_t tempSensorId = -1, int32_t humiditySensorId = -1);
  void begin();

  /*!
   *  @brief  Class that stores state and functions about Temperature
   */
  class Temperature : public Adafruit_Sensor {
  public:
    Temperature(DHT_Unified *parent, int32_t id);
    bool getEvent(sensors_event_t *event);
    void getSensor(sensor_t *sensor);

  private:
    DHT_Unified *_parent;
    int32_t _id;
  };

  /*!
   *  @brief  Class that stores state and functions about Humidity
   */
  class Humidity : public Adafruit_Sensor {
  public:
    Humidity(DHT_Unified *parent, int32_t id);
    bool getEvent(sensors_event_t *event);
    void getSensor(sensor_t *sensor);

  private:
    DHT_Unified *_parent;
    int32_t _id;
  };

  /*!
   *  @brief  Returns temperature stored in _temp
   *  @return Temperature value
   */
  Temperature temperature() { return _temp; }

  /*!
   *  @brief  Returns humidity stored in _humidity
   *  @return Humidity value
   */
  Humidity humidity() { return _humidity; }

private:
  DHT _dht;
  uint8_t _type;
  Temperature _temp;
  Humidity _humidity;

  void setName(sensor_t *sensor);
  void setMinDelay(sensor_t *sensor);
};

#endif

正如您在 github 存储库中看到的,这些文件具有 .cpp 和 .h 扩展名。

主项目依赖于一个用嵌入式 c 编写的谷歌库,并允许与谷歌云通信,到目前为止这还不是问题:https ://github.com/GoogleCloudPlatform/iot-device-sdk-嵌入式-c

任何人都可以启发我如何做到这一点?

非常感谢!

4

1 回答 1

1

是的,当我试图弄清楚如何通过 Cmake 链接 vs 库时,我的大脑也受到了伤害。

我是这样做的:

add_executable(exe_name main.cpp)

find_library(FLTK fltk /home/user/proj/fltk/fltk/lib)
find_library(FLTK_IMG fltk_images /home/user/proj/fltk/fltk/lib)

target_link_libraries(exe_name LINK_PUBLIC ${FLTK} ${FLTK_IMG} ${LINK_FLAGS})

所以对你来说,我认为你需要一个find_library但你必须指定库名称:

find_library(AF_SENSOR Adafruit_Sensor lib)
find_library(DHT_SENSOR DHT_sensor-library lib)
target_link_libraries(exe_name LINK_PUBLIC ${AF_SENSOR} ${DHT_SENSOR})

我猜“Adafruit_Sensor”是实际的 .lib 文件等。您可能需要 find_library() 的第三个参数的完整路径...

于 2021-05-15T15:19:21.497 回答