Class example C
Class example C
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.
-
- Posts: 9739
- Joined: Thu Nov 26, 2015 4:08 am
Re: Class example C
C doesn't (natively) support classes, you probably want C++. If you create files that end in .cpp, esp-idf will automagically interpret them as c++-files.
Re: Class example C
How can I switch it to C++?ESP_Sprite wrote: C doesn't (natively) support classes, you probably want C++.
- shabtronic
- Posts: 49
- Joined: Sun Nov 03, 2019 1:33 pm
Re: Class example C
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!
Re: Class example C
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!
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.
- shabtronic
- Posts: 49
- Joined: Sun Nov 03, 2019 1:33 pm
Re: Class example C
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.
Can't think of any pitfalls - but I've only been playing with the ADF/IDF for a short time. It supports
STL in cpp - well I've used <vector> <string> and <algorithm> and it's all totally awesome!
ADF/IDF functions don't need extern "C" wrappers to be used in cpp - but external libs that are c will
need the good old:
extern "C" {
#include "whackyoldClib.h"
}
e.t.c..
Just remembered, there is one almighty assache with cpp - and that's this:
in c you can do this:
i2s_stream_cfg_t i2s_cfg=Default_blah_blah_config();
in cpp it will error, you have to find the defined default config for that struct and do this:
i2s_cfg.type = AUDIO_STREAM_WRITER;//
i2s_cfg.task_prio = I2S_STREAM_TASK_PRIO;//
i2s_cfg.task_core = I2S_STREAM_TASK_CORE;//
i2s_cfg.task_stack = I2S_STREAM_TASK_STACK;//
i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE;//
i2s_cfg.i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX);//
i2s_cfg.i2s_config.sample_rate = 44100;//
i2s_cfg.i2s_config.bits_per_sample = (i2s_bits_per_sample_t)24;//
i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;//
i2s_cfg.i2s_config.communication_format = I2S_COMM_FORMAT_I2S;//
i2s_cfg.i2s_config.dma_buf_count = 3;//
i2s_cfg.i2s_config.dma_buf_len = 300;//
i2s_cfg.i2s_config.use_apll = 1;//
i2s_cfg.i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2;//
i2s_cfg.i2s_port = (i2s_port_t)0;
i2s_cfg.use_alc = false;
i2s_cfg.volume = 0;
i2s_cfg.multi_out_num = 0;
i2s_cfg.i2s_port = (i2s_port_t)0;//
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
Can't figure our the how to get around that!
Re: Class example C
Try starting with something like this:
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() {
}
Who is online
Users browsing this forum: Majestic-12 [Bot] and 127 guests