Class example C
Posted: Mon Dec 09, 2019 2:07 am
Is it possible to use classes when developing for the esp32? I can't seem to get the syntax correct when building the .h and .c files.
How can I switch it to C++?ESP_Sprite wrote: C doesn't (natively) support classes, you probably want C++.
rename your .c file to .cpp
That's Awesome, thanks shabtronic. I did that for my source code but ran into compilation errors. I simplified my source code to be an empty main and it's working now. I'll try to create a class and see how it goes.shabtronic wrote:rename your .c file to .cpp
put :
extern "C" {
void app_main(void);
}
in your .cpp file near the top
change cmakelists.txt to point at your new .cpp file instead of the old .c file
set(COMPONENT_SRCS "./main.cpp" )
idf.py fullclean
it should then build!
I think I had some initial problems going from c to cpp - I had to fullclean e.t.c. before it would work.Ziegler wrote: ↑Mon Dec 09, 2019 4:54 amThat's Awesome, thanks shabtronic. I did that for my source code but ran into compilation errors. I simplified my source code to be an empty main and it's working now. I'll try to create a class and see how it goes.shabtronic wrote:rename your .c file to .cpp
put :
extern "C" {
void app_main(void);
}
in your .cpp file near the top
change cmakelists.txt to point at your new .cpp file instead of the old .c file
set(COMPONENT_SRCS "./main.cpp" )
idf.py fullclean
it should then build!
Are there any pit falls I should be aware of? I guess this means that the ESP-IDF Source code/examples is all C code too. Will I ever need to do the changes above to any of those source files when calling them in my programs? Here's a copy of my code:
extern "C" {
void app_main(void);
}
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
void app_main() {
}
I guess all of the above .h files loaded fine so I'm wondering why I had compilation issues with my previous code.
Code: Select all
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
extern "C" void app_main() {
}