How to include C libraries in C++ code in esp-idf + Visual studio code

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?

You can’t just change the name and expect it to compile in C++. You can either do a conversion (there are on-line converters available) or you can create a C++ project and include the C code within a wrapper. In either case you should make that upgrade without any of the additions for the peripheral. Those additions can be done in small steps after you have proved that you have the existing code properly running with C++.

Oh ok, I see, but I have managed to get it to work now, as I modified my app_main to extern “C” app_main, and that fixed the issue. Thanks for the help though!