簡體   English   中英

如何使用 Cmake 在 c 項目中包含 c++ 庫

[英]How to include a c++ library in a c project using Cmake

我是 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

任何人都可以啟發我如何做到這一點?

非常感謝!

是的,當我試圖弄清楚如何通過 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() 的第三個參數的完整路徑...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM