-1

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!

tomascharad
  • 3,156
  • 23
  • 24
  • 2
    Your error message has nothing common with the "including other libraries". You need to add **your code** (C++ one) into the question, as like as the complete error message. Without them we simply cannot help you. – Tsyvarev May 15 '21 at 15:30
  • 1
    If the library only provides a C++ interface, you need to write a wrapper library in C++ that provides a C interface; the public headers for this wrapper library needs to be compatible with C, so the use of `class`es is out of the question. – fabian May 15 '21 at 16:18
  • Hi @Tsyvarev, I updated the question so it adds more details about the problem – tomascharad May 15 '21 at 17:06
  • 1
    @tomascharad - C doesn't recognize lots of things in C++, including the `class` keyword. You cannot directly use the libraries in your project. Either find/make a wrapper (as suggested by fabian) or switch your project to C++ instead of C. – Stephen Newell May 15 '21 at 17:16
  • 1
    Did you read the documentation of [cmake](http://cmake.org/) ? – Basile Starynkevitch May 15 '21 at 17:36
  • I've included now the C++ file @fabian – tomascharad May 15 '21 at 17:37
  • 1
    Complete error message about the **header** contains a **chain** of the include files involved, and contains a **source** file which is compiled. Please, provide the **complete error message**. So we no longer need to guess, whether you include a header into the C++ source file or into the C one. – Tsyvarev May 15 '21 at 19:39

1 Answers1

1

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() ...

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kevin
  • 398
  • 2
  • 7
  • The files are on .cpp and .h extensions, not .lib, would this means a different import mechanism? – tomascharad May 15 '21 at 17:03
  • Yes, they become part of the sources. For me, that'd mean adding them in the `add_executable` statement. – Kevin May 15 '21 at 19:34