Skip to main content
ferro
Lead
December 2, 2024
Question

How to tell CubeIDE to compile specific *.c files with C++ compiler ?

  • December 2, 2024
  • 5 replies
  • 3182 views

Hi,

How to tell CubeIDE to compile specific *.c files with C++ compiler ?

Changing suffix from *.c to *.cpp is not an option.

[edit] Changing the suffix from *.c to *.cpp is the least preferred option.

Thank you

5 replies

Andrew Neil
Super User
December 2, 2024

@ferro wrote:

Changing suffix from *.c to *.cpp is not an option.

 

 Why not?  Seems misleading to name it .c when it's actually C++ ?

Anyhow, Could you just #include  the .c file in a .cpp file, and exclude the .c file from build?

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.
ferro
ferroAuthor
Lead
December 2, 2024

Thanks for prompt reply.

No, files are writen in C and want to compile with C++ compiler.

"Could you just #include the .c file in a .cpp file, and exclude the .c file from build?"

Good idea but I perefere to set it as a flag in project properties. In Keil uVision IDE you right click on a file and set the  option to build it as C or C++.

Andrew Neil
Super User
December 2, 2024

If you right-click the file and choose 'Properties', there's this:

AndrewNeil_0-1733150322081.png

Looks like generic Eclipse ?

 

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.
Pavel A.
Super User
December 2, 2024

@ferro You already know about the -x switch of the GNU compiler. Use it. Compiler options can be modified per file.

/* disclaimer: this should not be taken as advise to mix C++ into Cube-generated main.c and compile Cube generated  C files in C++ mode */

ferro
ferroAuthor
Lead
December 3, 2024

"Compiler options can be modified per file."

Where, how ?

Lead II
December 3, 2024

You can exclude the c file from build and include it in a new cpp file. That's an ugly workaround.

 


@ferro wrote:

"Compiler options can be modified per file."

Where, how ?


Rightclick the file. Click properties. Click C/C++ General. Click Language mappings. Then select GNU C++ from the drop down menus.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
Ozone
Principal
December 3, 2024

C is basically a subset of C++, so a C souces should compile within a C++ project without issues.
They linkage is quite different (including startup file).
I suppose you are aware of the name mangling issue and the extern "C" statement in this context.

ferro
ferroAuthor
Lead
December 3, 2024

Thanks for comment, I would like to know how to configure CubeIDE.

Lead II
December 3, 2024

You can exclude the file from build and build it separately using a make file. You can run this command prior to building as an extra build step. Then add the o-file to the linker: https://community.st.com/t5/stm32-mcus-products/how-to-link-an-already-generated-object-file-in-existing-project/m-p/124589/highlight/true#M22428

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
ferro
ferroAuthor
Lead
December 3, 2024

Uf, this sounds scary. Seems the least annoying option is to change suffix to cpp, CubeIDE auto assign compiler based on that. And it's not possible to change it per file.

Lead II
December 3, 2024

@ferro wrote:

Uf, this sounds scary. Seems the least annoying option is to change suffix to cpp, CubeIDE auto assign compiler based on that. And it's not possible to change it per file.


Your first post said it was impossible to change the file extension. And now it suddenly is?

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
Lead II
December 4, 2024

And what happened? Can you show your build log?

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
ferro
ferroAuthor
Lead
December 5, 2024

Here, the original C recipe is used despite setting C++ in mapping.

ferro_0-1733405485312.png

I''ll come back later with a document describing what is going on.