Late to the game, but the answer is absolutely "yes".
I've written C++ on the ESP32 since day one (2.5 years ago) and never regretted the decision to do so. As always, you have to know your tool, so if you know C++, then there's no reason not use it on this platform imho. With the xtensa-gcc 8.2 compiler there's now support for C++17 so you have access to the new modern tools too.
Now that we're on the topic, I have to drop this shameless self-plug here in case you're wondering how people have taken C++ (and abstractions) on this platform.
Is it recommended/practical to use C++ instead of plain C ?
Re: Is it recommended/practical to use C++ instead of plain C ?
Cool, I will check your link.
EDIT: 'Know your tool' - absolutely & following.
I draw short of 'absolutely' if only to challenge the C die hards and to present 'appropriate use'. C++ includes C so technically you are correct (excluding designated intitialisation which really anoys me when porting).
C++ - yes. The problem is that people with limited C++ experience (but maybe have a soul & are C++ curious) then (because they have seen inappropriate use) read in inappropriate use!
Its like me quoting https://www.ioccc.org/ Yes that's some bad C but I am not saying C is bad! Must review & name that formal falacy!
So you could use a vector and reserve or you can push and dynamically allocate. Err, did you want a dynamic array? If so you have to pay for that.
So the argument goes that C++ is bad because it might malloc if you do not tell it not too. Well, err you make that call, reserve ffs!
The fact is that dynamic allocation will only happen if you do not use the library appropriately and/or want that flexiabiliy.
The answer? If you do not understand that library's impact then revert to 'C'. Learn one small C++ part at a time.
I do feel for those (as earlier posted) who have been sold C++ as the silver arrow without the broader plays being put in place (e.g. training).
Its a tool, invest in training, increase productivity and get over it! EDIT: Or (sans a genuine buisiness UP, watch your (most capable) team leave over time).
PS
I love C# and will burn C/C++ on the stake of 'reflection' once I have the ram etc for my embedded work (Moore's law/experience & ESP32 tells me that is not far off). My main drag (as I suspect for most) is time & that is an organisational/investment thing.
I will be in the car park should anyone wish to discuss further, just saying...
EDIT: 'Know your tool' - absolutely & following.
I draw short of 'absolutely' if only to challenge the C die hards and to present 'appropriate use'. C++ includes C so technically you are correct (excluding designated intitialisation which really anoys me when porting).
C++ - yes. The problem is that people with limited C++ experience (but maybe have a soul & are C++ curious) then (because they have seen inappropriate use) read in inappropriate use!
Its like me quoting https://www.ioccc.org/ Yes that's some bad C but I am not saying C is bad! Must review & name that formal falacy!
So you could use a vector and reserve or you can push and dynamically allocate. Err, did you want a dynamic array? If so you have to pay for that.
So the argument goes that C++ is bad because it might malloc if you do not tell it not too. Well, err you make that call, reserve ffs!
The fact is that dynamic allocation will only happen if you do not use the library appropriately and/or want that flexiabiliy.
The answer? If you do not understand that library's impact then revert to 'C'. Learn one small C++ part at a time.
I do feel for those (as earlier posted) who have been sold C++ as the silver arrow without the broader plays being put in place (e.g. training).
Its a tool, invest in training, increase productivity and get over it! EDIT: Or (sans a genuine buisiness UP, watch your (most capable) team leave over time).
PS
I love C# and will burn C/C++ on the stake of 'reflection' once I have the ram etc for my embedded work (Moore's law/experience & ESP32 tells me that is not far off). My main drag (as I suspect for most) is time & that is an organisational/investment thing.
I will be in the car park should anyone wish to discuss further, just saying...
& I also believe that IDF CAN should be fixed.
Re: Is it recommended/practical to use C++ instead of plain C ?
I use std::map, std::string, std::vector, find and iterators. The error messages are not nice and some things need just as much care for memory management as malloc/free because you can easily run out of PSRAM, it comes down to design and algorithm, and appropriate use and I would not criticise C++ when it just needs to be used appropriately.
It might be the style of programming, but when trying to use code an experienced colleague wrote in C++, it was verbose, bloated and slow. Getters and setters had everything seemingly written three times and an attempt to use boost XML serialisation failed miserably. It was hard to debug and the object orientation was annoyingly obtuse. Rewriting using csv instead of xml and using std::string and vector had the code running faster on the ESP32 than it did on a PC with an order of magnitude less memory use. Again picking the appropriate design rather than C++ criticism, but inappropriate design and C++ can go together.
I'm influenced by firmware in modern engine control units in terms of their reliability, safety, operational life. The memory and processing power are similar to an ESP32 (with a lot more I/O) and there is usually about 2-3MB of compiled C code. No dynamic allocation at all, no C++ or any classes in sight. Parts of the std library have really helped for the dynamic (eg webpage, server, file systems, running scripts) stuff on the ESP32, doing those in C99 would not be too much fun.
It might be the style of programming, but when trying to use code an experienced colleague wrote in C++, it was verbose, bloated and slow. Getters and setters had everything seemingly written three times and an attempt to use boost XML serialisation failed miserably. It was hard to debug and the object orientation was annoyingly obtuse. Rewriting using csv instead of xml and using std::string and vector had the code running faster on the ESP32 than it did on a PC with an order of magnitude less memory use. Again picking the appropriate design rather than C++ criticism, but inappropriate design and C++ can go together.
I'm influenced by firmware in modern engine control units in terms of their reliability, safety, operational life. The memory and processing power are similar to an ESP32 (with a lot more I/O) and there is usually about 2-3MB of compiled C code. No dynamic allocation at all, no C++ or any classes in sight. Parts of the std library have really helped for the dynamic (eg webpage, server, file systems, running scripts) stuff on the ESP32, doing those in C99 would not be too much fun.
-
- Posts: 9
- Joined: Tue Dec 31, 2019 1:45 am
Re: Is it recommended/practical to use C++ instead of plain C ?
Many thoughtful responses here, thanks to all! For those using C++, what IDE are you using? VSCode's plugin for ESP-IDF integration has so far cost me a lot of time, and that was just for C files. Which tools do you find work especially well for C++ on ESP32?
Re: Is it recommended/practical to use C++ instead of plain C ?
I saw a post from April 2020 indicating that ESP-IDF 4.3 supports a TLSF based heap memory allocation scheme. It mentions malloc, realloc, free, etc, but it makes no mention of C++ heap allocations resulting from `new` or as is used in STL types such as `std::string`, `std::vector`, etc.
I prefer to use C++ for application level programming on ESP32 using ESP-IDF. However, it's not clear to me whether there would still be a heap fragmentation issue if using some of the convenient types provided by the STL. Do these objects ultimately use `malloc`, `free`, etc?
I prefer to use C++ for application level programming on ESP32 using ESP-IDF. However, it's not clear to me whether there would still be a heap fragmentation issue if using some of the convenient types provided by the STL. Do these objects ultimately use `malloc`, `free`, etc?
Re: Is it recommended/practical to use C++ instead of plain C ?
Well, another question. If I want to the object-oriented character in ESP32 for somehow complex logic. Lua or C++, which should be select if considering robust and maintainability.
Re: Is it recommended/practical to use C++ instead of plain C ?
I honestly like C++ for all IoT programming and although I program mostly in C# what annoys me is lack of all needed features like multiple inheritance or inline assembly blocks. C# is good for humongous projects and devices with ton of memory and power.
Also VSCode is annoying for its lack of correct intellisense and smooth syntaxing unlike MS VS 2022 f.ex.
I wonder if anyone tried to install C++ for CLang for Risc-v etc on MS VS. Clang compiler is supposed to be much faster than gcc
Some engineer should unify those tools for across IDEs one day maybe
Also VSCode is annoying for its lack of correct intellisense and smooth syntaxing unlike MS VS 2022 f.ex.
I wonder if anyone tried to install C++ for CLang for Risc-v etc on MS VS. Clang compiler is supposed to be much faster than gcc
Some engineer should unify those tools for across IDEs one day maybe
Who is online
Users browsing this forum: yangxi and 79 guests