Skip to main content
May 27, 2025
Question

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

  • May 27, 2025
  • 6 replies
  • 1136 views

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.

6 replies

Andrew Neil
Super User
May 27, 2025
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
May 28, 2025

@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++

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Ozone
Principal
May 27, 2025

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.

TDK
Super User
May 27, 2025

You can't use classes in C files. If you want to use a class/object, it must be called from a C++ file.

"If you feel a post has answered your question, please click ""Accept as Solution""."
May 27, 2025

I wish ST provided us with a "main.cpp" and C++ versions of the libraries when we selected C++ as the target language. 

Ozone
Principal
May 28, 2025

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 ...

Andrew Neil
Super User
May 28, 2025

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Super User
May 28, 2025

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
May 28, 2025

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.