简体   繁体   中英

How to include a c++ library in a c project using Cmake

I'm a begginer on c, c++ and Cmake.

I've started from a project that allows me to send the data to Google Cloud IoT Core which is written in C: https://github.com/espressif/esp-google-iot/blob/master/examples/smart_outlet/README.md

As you can see I have a board with an ESP32 chip.

I bought an humidity sensor which I would like to use and the libraries are written in c++, so adding them to the C project has being very difficult. ( https://github.com/adafruit/DHT-sensor-library ) ( https://github.com/adafruit/Adafruit_Sensor )

I'm importing the libraries like this in my 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)

But I'm getting an error message when compiling the c++ libraries.

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

I think that the error is thrown because c++ classes are not supported on c.

This is the c++.h file that si throwing me the error:

#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

As you can see in the github repos, the files are with.cpp and.h extensions.

The main project has a dependency of a google library which is written in embedded c and allows to communicate with Google Cloud, which hasn't been a problem so far: https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c

Could anyone plis enlight me on how to do this?

Many thanks!

Yeah, my brain hurt too when trying to figure out how to link vs libraries via Cmake.

This is how I did it:

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})

So for you I'm thinking you need a find_library but you have to specify the library name:

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})

I'm guessing that "Adafruit_Sensor" is the actual.lib file, etc. You may need the full path for the 3rd argument to find_library()...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM