cancel
Showing results for 
Search instead for 
Did you mean: 

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

ferro
Senior III

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

22 REPLIES 22

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

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

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

AndrewNeil_0-1733150322081.png

Looks like generic Eclipse ?

 

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

Thanks. I set C++ there but adc.c is still treated as C.

ferro_1-1733152352370.png

 

Hm, Maybe clue is in what is writen at the bottom - seems that it is not per file.

 

ferro_0-1733152301338.png

If you can be bothered, could you try on your side, please ?

Pavel A.
Evangelist III

@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 */

Ozone
Lead II

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.

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

"Compiler options can be modified per file."

Where, how ?

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.