cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a C++ class in main.c

TechyAry
Associate

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.

12 REPLIES 12

> But others have no negative impact on memory usage or runtime behaviour and might be very useful on small systems as well.

No doubt - as long as there is sufficient performance and resources.
Maybe you were never involved in high-volume / low-cost projects, with managers insisting on lowest BOM costs.

Another point, although applicable to a certain niche, are toolchains and test tools.
My company's software projects involves SIL-2 certification, requiring more or less complete test coverage. And our test tool supported only plain C for a very long time.
Secondly, we use pre-certified libraries and RTOSes for most of our ECUs, which implies the toolchain used for exactly that certification. And none of those use C++.

TDK
Super User

I use C++ pretty exclusively for programming micros, and I keep main.c named main.c, and everything works fine.

Choose how to go downstream but don't fight the current. There are no showstoppers using C++ in the current CubeMX framework. main.h can be included if you need access to variables, and it has the __cplusplus guards.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Super User

C++ was lacking some useful features of C (for one, designated initializers) but newest C++ standard is going to make C++ a complete superset of C. When this happens, c++ will go mainstream in embedded. But even then... ants, cockroaches, sharks, crocodiles still exist and may outlast fancy creations. Long live C23.

By the way, the new AI-assisted Mojo language compiles native code for heterogeneous systems (like, CPU + GPU) and this is going to become a killer. Not Rust, sorry Microsoft.