C++ headers not found

MatzeM
Posts: 2
Joined: Thu Jul 23, 2020 7:28 am

C++ headers not found

Postby MatzeM » Thu Jul 23, 2020 8:36 am

I'm fairly new to ESP32 programming - so please forgive if it's a absolut beginners question...

I try to program ESP32 code with esp-idf v4.0.1 and C++ classes.
But it does not find the C++ includes; e.g. I have a MySingleton.h:
  1. #ifndef __MYSINGLETON_H__
  2. #define __MYSINGLETON_H__
  3.  
  4. #include <utility>
  5.  
  6. namespace MyNS {
  7.  
  8. template < typename T >
  9. class MySingleton {
  10. public:
  11.     template < typename... Args >
  12.     static constexpr T& Inst( Args... args_ ) {
  13.         static T instance( std::forward< Args >(args_) ... );
  14.         volatile int dummy{};
  15.         return instance;
  16.     }
  17.  
  18. protected:
  19.     T() = default;
  20.     ~T() = default;
  21.  
  22. private:
  23.     T( const T& ) = delete;
  24.     T& operator=( const T& ) = delete;
  25.     T( const T&& ) = delete;
  26.     T& operator=( const T&& ) = delete;
  27. };
  28. }
  29. #endif // __MYSINGLETON_H__
When I compile (with idf.py build) a component which uses it, I get:

Code: Select all

../include/Singleton.h:6:10: fatal error: utility: No such file or directory
 #include <utility>
          ^~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
I have a

Code: Select all

set(CMAKE_CXX_STANDARD 14)
in the CMakeLists.txt and a

Code: Select all

component_compile_options(-std=c++14)
in the component.mk - but in the compiler call I still see a

Code: Select all

---std=gnu99
?

I also have problems understanding how I correctly manage my directory structure with CMakeLists.txt
I have my base directory and inside it a "src", a "main" and a "include" directory.
In "include" I put all my *.h files in which needs to be used by others
In "main" there is only "main.c"
In "src" there are currently all my other modules (*.cpp and internal *.h).
In which of the directories do I need CMakeLists.txt files and in which compontent.mk files and what should their contents be?

MatzeM
Posts: 2
Joined: Thu Jul 23, 2020 7:28 am

Re: C++ headers not found

Postby MatzeM » Tue Jul 28, 2020 11:26 am

In the meantime I went a step further - but it's still not solved.

I now have the following directory structure inside my project dir:

Code: Select all

include
main
MyWifi
My CMakeLists.txt in the base project dir looks like follows:

Code: Select all

cmake_minimum_required( VERSION 3.10 )
set( CMAKE_C_STANDARD 11 )
set( CMAKE_CXX_STANDARD 17 )

if ( ${ESP_PLATFORM} )
  FILE( TO_CMAKE_PATH "$ENV{IDF_PATH}" idf_path )
  include( ${idf_path}/tools/cmake/project.cmake )
  set( EXTRA_COMPONENT_DIRS
                        MyWifi )

  # Projektname:
  project( MyProject )
else() # ! ESP_PLATFORM
  # empty...
endif()
The CMakeLists.txt in the main directory is this:

Code: Select all

cmake_minimum_required( VERSION 3.10 )
set( CMAKE_C_STANDARD 11 )
set( CMAKE_CXX_STANDARD 17 )

FILE( GLOB_RECURSE my_sources *.cpp *.h *.hpp )
FILE( TO_CMAKE_PATH "$ENV{IDF_PATH}" idf_path )

if ( ${ESP_PLATFORM} )
  idf_component_register(
    SRCS                ${my_sources}
    INCLUDE_DIRS        "${CMAKE_CURRENT_LIST_DIR}"
                        "${CMAKE_SOURCE_DIR}/include"
                        "${CMAKE_SOURCE_DIR}"
                        "${idf_path}/components"
                        "${idf_path}"
    REQUIRES            MyWifi )
endif()
The CMakeLists.txt in the MyWifi directory is this:

Code: Select all

cmake_minimum_required( VERSION 3.10 )

set( CMAKE_C_STANDARD 11 )
set( CMAKE_CXX_STANDARD 17 )

FILE( GLOB_RECURSE my_sources *.cpp *.h *.hpp )

FILE( TO_CMAKE_PATH "$ENV{IDF_PATH}" idf_path )

if ( ${ESP_PLATFORM} )
  idf_component_register(
    SRCS                ${my_sources}
    INCLUDE_DIRS        "${CMAKE_CURRENT_LIST_DIR}"
                        "${CMAKE_SOURCE_DIR}/include"
                        "${CMAKE_SOURCE_DIR}"
                        "${idf_path}/components"
                        "${idf_path}"  )
endif()
In every of the above directories there is an empty component.mk

The following source files exist:

Code: Select all

sdkconfig
version.txt
include/MyWifi.hpp
include/Singleton.hpp
main/main.cpp
MyWifi.cpp
Here is include/Singleton.hpp:

Code: Select all

#ifndef __MY__SINGLETON_H__
#define __MY__SINGLETON_H__

namespace NS1 {
namespace SubNS1 {

template < typename T >
class CSingleton
{
    public:
        template < typename... Args >
        static constexpr T& Inst(   Args... args_ ) {
            static T instance( std::forward< Args >(args_)... );

            // damit bei Optimierung die Methode nicht wegrationalisiert wird:
            volatile int dummy{};

            return instance;
        }


    protected:
                        T()                         = default;
                        ~T()                        = default;


    private:
                        T(          const T& )      = delete;
        T&              operator=(  const T& )      = delete;
                        T(          const T&& )     = delete;
        T&              operator=(  const T&& )     = delete;
}; // END_CLASS CSingleton

}
}

#endif
And the include/MyWifi.hpp:

Code: Select all

#ifndef __MY__MYWIFI_H__
#define __MY__MYWIFI_H__

#include "Singleton.hpp"


namespace NS1 {
namespace SubNS1 {

class CMyWifi final
    : public CSingleton<CMyWifi>
{
    public:
        void        Init( void );
        void        Connect(        void );
        void        Scan(           void );

        void        TaskKeepAlive(  void*   pParam_ );

    private:
        CMyWifi()
            : CSingleton() {
        }
}; // END_CLASS CMyWifi

}
}

static NS1::SubNS1::CMyWifi&       g_WiFi = SD::MOC::CMyWifi::Inst();

#endif
The MyWifi/MyWifi.cpp is currently only a nearly empty class:

Code: Select all

#include "MyWifi.hpp"

#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>

#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <esp_wifi.h>
//#include <esp_wpa2.h>

//#include <nvs_flash.h>


#define WIFI_CONNECT_SSID           "MySSID"
#define WIFI_CONNECT_PASSWD         "MySSIDPasswd"

#define WIFI_AP_SSID                "MyAPSSID"
#define WIFI_AP_PASSWD              "MyAPSSIDPasswd"

// FreeRTOS event group to signal when we are connected and
//   ready to make a request:
static EventGroupHandle_t           WifiEventGroup;

// The event group allows multiple bits for each event, but we only care about
//   one event - are we connected to the AP with an IP?
const int CONNECTED_BIT = BIT0;


namespace NS1 {
namespace SubNS1 {

void CMyWifi::Init()
{
    tcpip_adapter_init();
    WifiEventGroup = xEventGroupCreate();

    ESP_ERROR_CHECK( esp_event_loop_create_default() )

    wifi_init_config_t wifiInitCfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&wifiInitCfg) );
}


void CMyWifi::Connect()
{
}


void CMyWifi::Scan()
{
}


void TaskKeepAlive(                 void *pParam_ )
{
    for ( ; ; ) {
    }
}

}
}
main/main.cpp is this:

Code: Select all

#include "MyWifi.hpp"

#include <esp_err.h>
//#include <nvs.hpp>
//#include <nvs_flash>


#ifdef __cplusplus
extern "C" {
#endif

void app_main( void )
{
    // Initialize NVS
    esp_err_t ret;

    ret = nvs_flash_init();
    if ( ret == ESP_ERR_NVS_NO_FREE_PAGES ||
         ret == ESP_ERR_NVS_NEW_VERSION_FOUND ) {
        ESP_ERROR_CHECK( nvs_flash_erase() );
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    //g_WiFi.Init();
}

#ifdef __cplusplus
} // extern "C"
#endif
I'm on Windows 10 Pro and try to compile it with esp-idf v4.0.1 via command line (%IDF_PATH%\export.bat has been executed):

Code: Select all

> idf.py -v --ccache build
After a longer compilation of the esp-idf itself it came to my directory and it doesn't matter if I use #include <nvs.hpp> or #include <nvs.h> or if I add #include <nvs_flash.h> I always get errors, that the include files could not be found:

Code: Select all

[2/12] cmd.exe /C "cd /D D:\Prog\MyProject\build\bootloader && D:\Prog\ESP32\esp-idf\Tools\tools\cmake\3.13.4\bin\cmake.exe --build ."
ninja: no work to do.
[3/10] ccache D:\Prog\ESP32\esp-idf\Tools\tools\xtensa-esp32-elf\esp-2019r2-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe   -Iconfig -I../main -I../include -I../ -ID:/Prog/ESP32/esp-idf/v4/components -ID:/Prog/ESP32/esp-idf/v4 -ID:/Prog/ESP32/esp-idf/v4/components/newlib/platform_include -ID:/Prog/ESP32/esp-idf/v4/components/freertos/include -ID:/Prog/ESP32/esp-idf/v4/components/heap/include -ID:/Prog/ESP32/esp-idf/v4/components/log/include -ID:/Prog/ESP32/esp-idf/v4/components/soc/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/soc/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_rom/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_common/include -ID:/Prog/ESP32/esp-idf/v4/components/xtensa/include -ID:/Prog/ESP32/esp-idf/v4/components/xtensa/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/driver/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_ringbuf/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_event/include -ID:/Prog/ESP32/esp-idf/v4/components/tcpip_adapter/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/include/apps -ID:/Prog/ESP32/esp-idf/v4/components/lwip/include/apps/sntp -ID:/Prog/ESP32/esp-idf/v4/components/lwip/lwip/src/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/port/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/port/esp32/include/arch -ID:/Prog/ESP32/esp-idf/v4/components/vfs/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_wifi/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_wifi/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_eth/include -ID:/Prog/ESP32/esp-idf/v4/components/efuse/include -ID:/Prog/ESP32/esp-idf/v4/components/efuse/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/app_trace/include -I../MyWifi -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -Wno-parentheses -Wno-sizeof-pointer-memaccess -Wno-clobbered -Wno-format-overflow -Wno-stringop-truncation -Wno-misleading-indentation -Wno-cast-function-type -Wno-implicit-fallthrough -Wno-unused-const-variable -Wno-switch-unreachable -Wno-format-truncation -Wno-memset-elt-size -Wno-int-in-bool-context -fstack-protector-all -std=gnu++11 -fexceptions -fno-rtti -D_GNU_SOURCE -DIDF_VER=\"v4.0.1-316-g48ea44f3d-dirty\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -std=gnu++17 -MD -MT esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -MF esp-idf\main\CMakeFiles\__idf_main.dir\main.cpp.obj.d -o esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -c ../main/main.cpp
FAILED: esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj
ccache D:\Prog\ESP32\esp-idf\Tools\tools\xtensa-esp32-elf\esp-2019r2-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe   -Iconfig -I../main -I../include -I../ -ID:/Prog/ESP32/esp-idf/v4/components -ID:/Prog/ESP32/esp-idf/v4 -ID:/Prog/ESP32/esp-idf/v4/components/newlib/platform_include -ID:/Prog/ESP32/esp-idf/v4/components/freertos/include -ID:/Prog/ESP32/esp-idf/v4/components/heap/include -ID:/Prog/ESP32/esp-idf/v4/components/log/include -ID:/Prog/ESP32/esp-idf/v4/components/soc/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/soc/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_rom/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_common/include -ID:/Prog/ESP32/esp-idf/v4/components/xtensa/include -ID:/Prog/ESP32/esp-idf/v4/components/xtensa/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/driver/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_ringbuf/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_event/include -ID:/Prog/ESP32/esp-idf/v4/components/tcpip_adapter/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/include/apps -ID:/Prog/ESP32/esp-idf/v4/components/lwip/include/apps/sntp -ID:/Prog/ESP32/esp-idf/v4/components/lwip/lwip/src/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/port/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/lwip/port/esp32/include/arch -ID:/Prog/ESP32/esp-idf/v4/components/vfs/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_wifi/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_wifi/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/esp_eth/include -ID:/Prog/ESP32/esp-idf/v4/components/efuse/include -ID:/Prog/ESP32/esp-idf/v4/components/efuse/esp32/include -ID:/Prog/ESP32/esp-idf/v4/components/app_trace/include -I../MyWifi -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -Wno-parentheses -Wno-sizeof-pointer-memaccess -Wno-clobbered -Wno-format-overflow -Wno-stringop-truncation -Wno-misleading-indentation -Wno-cast-function-type -Wno-implicit-fallthrough -Wno-unused-const-variable -Wno-switch-unreachable -Wno-format-truncation -Wno-memset-elt-size -Wno-int-in-bool-context -fstack-protector-all -std=gnu++11 -fexceptions -fno-rtti -D_GNU_SOURCE -DIDF_VER=\"v4.0.1-316-g48ea44f3d-dirty\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -std=gnu++17 -MD -MT esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -MF esp-idf\main\CMakeFiles\__idf_main.dir\main.cpp.obj.d -o esp-idf/main/CMakeFiles/__idf_main.dir/main.cpp.obj -c ../main/main.cpp
../main/main.cpp:4:10: fatal error: nvs.h: No such file or directory
 #include <nvs.h>
          ^~~~~~~
compilation terminated.
So in principal: isn't it not possible to program in C++ for ESP-IDF? Or what's the problem?

Who is online

Users browsing this forum: Baidu [Spider] and 198 guests