Class example C

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Class example C

Postby Ziegler » 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.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: Class example C

Postby ESP_Sprite » Mon Dec 09, 2019 2:23 am

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.

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Re: Class example C

Postby Ziegler » Mon Dec 09, 2019 3:40 am

ESP_Sprite wrote: C doesn't (natively) support classes, you probably want C++.
How can I switch it to C++?

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

Re: Class example C

Postby shabtronic » Mon Dec 09, 2019 3:58 am

Ziegler wrote:
Mon Dec 09, 2019 3:40 am
ESP_Sprite wrote: C doesn't (natively) support classes, you probably want C++.
How can I switch it to 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!

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Re: Class example C

Postby Ziegler » Mon Dec 09, 2019 4:54 am

shabtronic wrote:
Ziegler wrote:
Mon Dec 09, 2019 3:40 am
ESP_Sprite wrote: C doesn't (natively) support classes, you probably want C++.
How can I switch it to 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!
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.

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.

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

Re: Class example C

Postby shabtronic » Mon Dec 09, 2019 5:02 am

Ziegler wrote:
Mon Dec 09, 2019 4:54 am
shabtronic wrote:
Ziegler wrote:
Mon Dec 09, 2019 3:40 am


How can I switch it to 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!
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.

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.
I think I had some initial problems going from c to cpp - I had to fullclean e.t.c. before it would work.

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!

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Class example C

Postby ESP_Angus » Mon Dec 09, 2019 5:38 am

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 142 guests