Skip to main content
truland
Associate
June 30, 2026
Solved

VS Code - use g++ for main.c

  • June 30, 2026
  • 4 replies
  • 72 views

Hi,

I wanted to give Visual Studio Code a try and converted an existing STM32CubeIDE project. In the original project, the main.c (in Core/Src directory) file was compiled using g++, since it has some C++ content. However, VS Code invokes gcc, and therefore, build is not successful. How can I change this?

One other thing, I noticed when converting the project, all source files are copied to the new location, but the .ioc file is not. Is this intended behaviour?

Best answer by svogl

well, if you want to stick to cubemx, then why not move your code to a separate cpp file & place a simple function call the main.c user section?

4 replies

svogl
Associate III
July 1, 2026

did you try renaming main.c to main.cpp? that should trigger the c++ compiler.

Simon

truland
trulandAuthor
Associate
July 6, 2026

Well, sure if I rename the file ist will work, however if I change something in CubeMX, a new main.c will be generated, and the two files will have to be merged manually. So I think this is not a good long-term solution

svogl
svoglBest answer
Associate III
July 6, 2026

well, if you want to stick to cubemx, then why not move your code to a separate cpp file & place a simple function call the main.c user section?

unsigned_char_array
Lead III
July 6, 2026

since it has some C++ content

 

Why did you put C++ code in a C-file?

Just create a separate cpp entry function you call from main.c.

"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."
truland
trulandAuthor
Associate
July 7, 2026

Yes, I know that’s a clean solution, thank you. I was just wondering that the CudeIDE’s project setting to use g++ for compiling main.c was abandoned when migrating the project to VS code.

Apart from that, I’m quite sure it’s possible to change the behaviour with CMake, I just don’t know how to do this. 

Anyway, I’m seeing my problem as resolved now.

Andrew Neil
Super User
July 7, 2026

I’m seeing my problem as resolved now.

Great! So now please mark the solution on the post which provided the resolution - not this one!

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.