So I had created a project from the template Hello_World project template that is coded in C. Everything was working fine, but today, I needed to include this library to code a peripheral, and the library is only available in C++. From what I read online, it is quite easy to include a C header file (.h) in a C++ file, but it is almost impossible to include a C++ header file (.hpp) in a C file. So, I changed my “ProjectName.c” file to “ProjectName.cpp” file. I updated my CMakeLists.txt in my “main” folder accordingly to:
idf_component_register(SRCS "ProjectName.cpp"
INCLUDE_DIRS ""
REQUIRES I2Cbus; MPUdriver; spi_flash)
target_compile_options(${COMPONENT_LIB} PRIVATE -std=c++20)
The code for the #includes in my ProjectName.cpp is:
extern "C" {
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "driver/uart.h"
}
#include "I2Cbus.hpp"
#include "MPU.hpp" // main file, provides the class itself
#include "mpu/math.hpp" // math helper for dealing with MPU data
#include "mpu/types.hpp" // MPU data types and definitions
However, now when i try to build my project, it fails with the following error:
[530/532] Linking CXX executable Project1.elf
FAILED: Project1.elf
(Project1 is the name of the overall project)
The project could build before I changed the file to a C++ file. What am I doing wrong, and how can I fix this?