2025-05-27 3:12 AM
What instructions do you follow to implement a C++ class and use it in "main.c"?
Suppose it's a simple project, like blinking the LED.
2025-05-27 3:18 AM
Once you've got the tools set up to build C++, it should be no different to mixing C and C++ in any other context...
https://isocpp.org/wiki/faq/mixing-c-and-cpp
https://stackoverflow.com/questions/4025869/using-mixing-c-in-c-code
2025-05-27 3:47 AM
I think at this point it's too late.
You would need to create a C++ project, which would name the file "main.cpp".
It works the other way around, C++ can call standard C code.
Neither does the C startup code come with constructors/desctructors, nor does the linker deal well with name mangling issues.
2025-05-27 6:11 AM
You can't use classes in C files. If you want to use a class/object, it must be called from a C++ file.
2025-05-27 7:04 AM
I wish ST provided us with a "main.cpp" and C++ versions of the libraries when we selected C++ as the target language.
2025-05-27 11:08 PM
First, I don't use CubeIDE, nor Cube/HAL code.
But the IDE / toolchain can handle C++ projects as well as C ones', just create new projects as such.
The proper compiler/linker settings and startup code will be automatically selected.
> ...and C++ versions of the libraries ...
You can use "C code" libraries in C++, although you lose the C++ specific advantages.
Why there are no cpp libraries and most (if not all) examples are plain C, you would need to ask ST ...
2025-05-28 1:25 AM
@Ozone wrote:Why there are no cpp libraries and most (if not all) examples are plain C, you would need to ask ST ...
Demand, I guess - or lack thereof.
2025-05-28 1:49 AM
@Andrew Neil wrote:no different to mixing C and C++ in any other context...
As @TDK said, you can't ever use C++ classes in C files - the C language has no concept of classes.
It's the (a?) key thing which C++ adds and C does not have.
This has nothing to do with ST or STM32 or CubeIDE - it is a fundamental limitation of the language.
But, as @TDK and @Ozone, you can call C functions and access C data from C++
2025-05-28 1:50 AM
> Demand, I guess - or lack thereof.
Which will probably remain so.
While C++ has quite a few desirable features, most embedded system on Cortex M level imply a static memory setup, and careful planning. While it could be done with C++ (or reduced subsets - anybody remembering "EC++" ?), there is definitive a penalty comared to plain C.
It seems the most frequent use case are systems with a LCD / GUI (usually based upon C++ classes), and expensive external memory.
2025-05-28 3:32 AM
@Ozone wrote:> Demand, I guess - or lack thereof.
Which will probably remain so.
While C++ has quite a few desirable features, most embedded system on Cortex M level imply a static memory setup, and careful planning. While it could be done with C++ (or reduced subsets - anybody remembering "EC++" ?), there is definitive a penalty comared to plain C....
C++ has many extensions going beyond plain C.
Some of them must be handled with care on small embedded systems, this is true, including dynamic types (set, map...) or virtual methods.
But others have no negative impact on memory usage or runtime behaviour and might be very useful on small systems as well. An example is using fixed point data or saturated numbers. In C++ this can be expressed much better and more readable than in C.
Another example is you can write a constructor for a struct to have it initialized for sure.
So C++ can be really useful if used wisely and may improve the code significantly. Pointing out possibly bad features is no reason to avoid real improvements...