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

25 REPLIES 25

Okay, you did not. Thanks.

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.

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.

I created a simple example project using STM32CubeMX 6.12.1.

I opened it in STM32CubeIDE 1.16.0 and converted it to cpp (rightclick convert to cpp). I added 2 source files:

  • test.c
  • test_cpp.cpp

I tried various ways to override the file settings and language mappings and even editing the .cproject xml file manually (copying the settings of the cpp file from the xml). It doesn't work. So strange. The only thing that worked was modifying the build command to g++. But this doesn't take any of the project wide C++ settings and defines. It even compiles it with -std=gnu11. So this is ugly but it works.

I tried something interesting. The properties of the file test.c showed "C Source File" regardless of my language mapping. So Eclipse uses some type of pattern filter based on file extension to determine the type. I was able to override this:

  • on the project: rightclick properties
  • C/C++ General
  • File Types
  • Use project settings
  • File Types
  • add: filename=test.c, Type=C++ Source File

Now it shows the correct type. And the language mapping tab is automatically set to "C++ Source File". However in build it still shows the C compiler. Even after deleting the source file and adding it again. So it seems Eclipse uses the associated source type only for some purposes (syntax highlighting?) and not for actual compiler language selection. And language mapping is ignored.

unsigned_char_array_0-1733480653686.png

unsigned_char_array_1-1733480702724.png

unsigned_char_array_2-1733480722087.png

So "type" doesn't do anything.

my test.c source file:

 

 

#ifdef __cplusplus
#warning "test.c compiled as C++"
#else
#warning "test.c  compiled as C"
#endif

 

 

Edit:
I also added a file "test.test" and made an association with C++. It doesn't compile and it doesn't highlight syntax. So I have to clue what eclipse uses "type" for.

 

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.

Thank you very much for this. Great reference.

"The only thing that worked was modifying the build command to g++. But this doesn't take any of the project wide C++ settings and defines."
I am also trying to find out if it is possible to access a build variable, if there is such, that hold all C++ or C options.

ferro_0-1733487641017.png