Error: unknown type name 'class'

The PlatfromIO compiler does not recognize the assignment of classes. NOT Arduino !!!

Compiling .pio\build\esp32dev\esp-idf\src\main.c.o
In file included from src\main.c:12:
C:/PlatformIO/libs/rAHT1x32/include/rAHT1x32.h:51:1: error: unknown type name ‘class’
class AHT1x {
^~~~~
C:/PlatformIO/libs/rAHT1x32/include/rAHT1x32.h:51:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘{’ token
class AHT1x {
^

1 Like

You are including a C++ class from a C file, main.c. This can impossibly work.

And it turns out that it is impossible to use classes on the ESP-IDF? Some kind of garbage …

Yes ESP-IDF works with classes.

Your mistake is just that you have the file main.c in which you include a header file that has the class keyword. The C language has no classes, C++ has. This is not a fault of the ESP-IDF framework.

Please see Standard C++. It is well possible to interface C and C++ code with the right technique (extern "C", #ifdef __cplusplus and friends). There’s also an ESP-IDF + C++ example at Using c++ with ESP-IDF for ESP32 development.

There are also C++ + ESP-IDF examples available at platform-espressif32/examples/espidf-exceptions at develop · platformio/platform-espressif32 · GitHub.

I’ve also written a simple test project with one class and it works fine. The trick is just that if someone whishes to use C++ classes, they better be using C++ and include it from a .cpp file, or use one of the techniques listed above for interfacing. C++ can code very easily call into C code.

As a suggestion, if C++ code is to be introduced into a previously C only codebase, you can rename all C files to .cpp and fix the compile errors (there should only be a few – most stuff is compatible), and then you can automatically use C++ code; or, only change the main.c file to C++ and include all the C specific code with

extern "C" {
 #include "c-library-1.h" 
 #include "c-library-2.h"
}

and then use C++ code in the now main.cpp.

To make C code robust against inclusion by C++ code, do as linked above, by using the

#ifdef __cplusplus
extern "C" {
#endif

/* original C header content */

#ifdef __cplusplus
}
#endif 

style.

Note that this is what the ESP-IDF framework already does for most of its headers, that’s why it can be nicely used with C++ without much hassle.

I got it, thanks a lot