cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure STM32CubeMX to support C++ development?

Ade J.3
Associate II

When I use CubeMX, the default language of my genereted code is C. I would like to develop in C++, as I can do in STM32CubeIDE. (I want to use VSCode to program)

Is there any option to do this? I've tried just change the main.c to main.cpp, but it didn't work.

(I'm a noob. So, if this questions is elementar, please, let me know likewise).

1 ACCEPTED SOLUTION

Accepted Solutions
Ghofrane GSOURI
ST Employee

Hello @Ade J.3​ 

Actually, The IDE supported by the CubeMX are: STM32CubeIDE, EWARM, MDK ARM and Makefile .

VS Code is not supported by CubeMX.

Thx

Ghofrane

View solution in original post

7 REPLIES 7
Ghofrane GSOURI
ST Employee

Hello @Ade J.3​ ;

First of all let me thank you for posting.

While C may be the preferred language of many embedded programmers, C++ does have a place, especially if you want to use certain libraries.

 I will show you how to get started making a program (blinky) for an STM32 microcontroller using C++ on STM32CubeIDE.

I will use an ST Nucleo-L432KC for this demonstration, as it’s small, fits on a breadboard, and still has a powerful ARM Cortex-M4 core. You are welcome to use any STM32 part, but I generally recommend starting out with one of the Nucleo boards.

Plug your STM32 part into your computer.

Start STM32CubeIDE[link] and select File > New > STM32 Project. Select your target microcontroller or board (I’ll select my Nucleo-L432KC).

0693W00000VOhhZQAT.pngClick Next and then give your project a name. I like to prefix the board I’m using to the project name, so nucleo-l432-cpp-blinky is what I called mine. In Targeted Language, select C++.

0693W00000VOhiSQAT.pngClick Finish and click Yes when asked, “Initialize all peripherals with their default Mode?�? Click Yes to open the new perspective.

Leave everything as default on the CubeMX device configuration GUI.

Code should be automatically generated. However, CubeMX generates a main.c file, which is not what we want for a C++ project. Right click on <project name> > Core > Src > main.c and select Rename. Change the name of the file to main.cpp. The compiler is sensitive to file suffixes, so .c files will compile as C code and .cpp or .cc files will compile as C++.

0693W00000VOhkiQAD.pngOpen the main.cpp file associated with the project. Scroll down to the while (1) loop and add the following code just inside the while (1) loop:

HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
 
HAL_Delay(500);

0693W00000VOhhzQAD.pngNote that the GPIO port and pin names might be different for your board. If you’re using a Nucleo-64, 

In case you are wondering: yes, this is still C inside of a C++ file. Most of the STM32 HAL libraries are written in C, but they’re safe to call in C++.

Save and click Project > Build Project. You should notice that any .cpp or .cc files get automatically built with g++.

0693W00000VOhmtQAD.pngClick Run > Debug and click OK to accept the default launch configuration properties. Accept any prompts you might see to open the debug perspective. Click Run > Resume, and you should see the LED begin to blink.

Ghofrane

Ade J.3
Associate II

Thank you for your complete answer!

However, I would like to use another IDE, like VS Code. Is there any way to initialize the peripherals in CubeMX and after keep coding in another ide in C++?

Ghofrane GSOURI
ST Employee

Hello @Ade J.3​ 

Actually, The IDE supported by the CubeMX are: STM32CubeIDE, EWARM, MDK ARM and Makefile .

VS Code is not supported by CubeMX.

Thx

Ghofrane

Okay. Actually, there is an not official extension that works well with C, called stm32 for VSCode. But in C++ it's a little bit hard to work.

Once again, thanks for your answer.

Hi @Ghofrane GSOURI 

The above description works fine with STM32CubeIDE.

But when I try to add the same thing with KEIL uVision it gives a lot of error messages.

I'm generating my file in STM32CubeMX for Keil and then disabling the main.c build and adding main.cpp file

AVerm4_3-1707069306492.png

 

AVerm4_2-1707069277104.png

 

 

AVerm4_1-1707069211354.png

 

AVerm4_0-1707069186744.png

 

As you may know Ghofrane, VScode is a text editor and at least entirely compatible as a piece of this developers toolchain. As of 2024 we now have PlatoformIO and on OSX is very useful for me an hundreds of other STM devs. Also Makefile is not an IDE. Why do you provide such an unhelpful and frankly mean response to an honest question? Especially as an ST employee?
Your answer was non-sensical and I would imagine your are an artificial response. If not please do better on here. This is a place to support not provide meaningless nonsense. Thankyou

wj
Associate

You can use the entensions named platformio or eide in VSCode, they are both support STM32

截屏2024-04-28 16.09.59.png

截屏2024-04-28 16.11.04.png

As for using C++ in embedded projects, this is certainly possible.

Next, I'll use platformio to implement mixed C/C++ programming in projects generated by STM32CubeMX.

First, generate a template project with makefile, cmake or other which using arm-none-eabi-gcc.

截屏2024-04-28 16.18.32.png

Second, open the folder with VSCode, add a file named "platformio.ini" in root directory, and Incorporate the following:

 

; 在配置文件中分号开头为注释
; In the configuration file, a semicolon starts with a comment
[platformio]
; 将源代码的编译目录移到项目目录下,否则默认src下
; Move the compilation directory of the source code to the project directory, otherwise it will be under src by default
src_dir = ./

[env:genericSTM32F103RC]
; 配置平台和板子
; configure the platform and board
platform = ststm32
board = genericSTM32F103RC

; the option will add same drive to your project
; if your project is generated by STM32CubeMX and you use the option, there will be conflicts
; framework = stm32cube

; debug mode or release mode(unnecessary)
build_type = debug

; 编译配置-D是宏定义,-Idir是include文件目录,读者可按自己项目结构更改
; the start with -D are global macro definitions(by default 1 without "= sth.")
; the start with -I are include path
; using -std to choose your C/C++ version
build_flags =
  -O1
  -std=c++17
  -Wl,-u,_printf_float
  -DSTM32F103xE
  -DUSE_FULL_LL_DRIVER
  -Dregister=
  -ICore/Inc
  -Icpp_core/inc
  -IDrivers/CMSIS/Include
  -IDrivers/CMSIS/Device/ST/STM32F1xx/Include
  -IDrivers/STM32F1xx_HAL_Driver/Inc
  -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy

; 选择编译文件的所在路径,这里包含了源文件路径,启动文件,驱动库和rtos路径。如果+<*>便是路径下的所以文件,-<.git/>便是忽略.git路径下的文件
; these are source file(include .c, .cpp, .asm, .s)
; If there is a '/' after the path, all the source files in it are recursively appended
; the +<*> is add file, -<.git/> ignores files in the .git path
build_src_filter = +<Core/Src> +<cpp_core/src> +<startup_stm32f103xe.s> +<Drivers/> +<bsp/> +<fmt/> +<String/>

; 选择链接文件
; your link file
board_build.ldscript = STM32F103VETx_FLASH.ld

; 选择下载的仿真器,这里是选择的jlink,如果不设置默认stlink
; your upload protocol, by default with stlink
upload_protocol = cmsis-dap

; 字面意思
; your debug tool, by default with none
debug_tool = stlink

 

 Second, add a folder(any name, I named it "cpp_core") with 2 subfolders named inc, src, and add new files cpp_main.cpp, cpp_main.hpp, cpp_it.cpp here.

截屏2024-04-28 16.47.09.png

 Third, include "main.h" in cpp_main.h, add the defination of a function named "extern "C" void cpp_main(void)" in cpp_main.cpp, and its declare in main.h(not cpp_main.hpp). Add the function in your main function.

截屏2024-04-29 15.11.44.png

截屏2024-04-29 15.13.32.png

截屏2024-04-29 15.14.08.png

截屏2024-04-29 15.15.19.png

 Now, You can use "cpp_main" as your new main function in C++.