10
votes

I would like to create a project for STM32 with HAL in C++. Unfortunately, CubeMX supports only projects in C. However, HAL support C++.

I have created a project in CubeMX and I was trying to import it to any of the IDEs above but without any success because it is imported as a C project. So it doesn't look like a good solution.

Also, I have tried creating a C++ project in the mentioned IDE and adding to it files from CubeMX. It seems to be more logic but I can't do it in the right way because my configuration is still wrong.

Could anyone explain me how should I configure new C++ projects in Eclipse or Atollic TrueSTUDIO in order to use HAL?

4
Question too generic, insert some details. What have you tried?Piero Alberto
Google helped me to find you a stm32 related page that uses eclipse.<br> <a href="carminenoviello.com/en/2015/06/04/… to tutorial</a>Gull_Code
Don't use the STlib ("HAL"). It ist just bloat, but does not provide true abstraction from the hardware. It only adds another layer of complexity. You still have to know the hardware and have the ref-man at hand, but with HAL you have to learn that too and additionally know how that translates to the hardware. Instead encapsulate hardware accesses in drivers with your own interface.too honest for this site
Don't really listen to Olaf. HAL is pretty good and clean. Even if you always have to hold ref-man while you are developing something. HAL saves your time a lot if you READ someone else code. Even if that someone is yourself but a while ago)MajesticRa

4 Answers

9
votes

Recent versions of CubeMX support System Workbench 4 STM32, which is an Eclipse-based IDE pre-configured to develop for STM32 targets. It has the option to convert a project to C++ (Right-click on the project in the project explorer, then select "Convert to C++").

Note that I have no direct experience using this method. We use the STM HAL and build our C++ applications on top of that, so this is definitely possible, but our IDE is Keil, so YMMV.

7
votes

How to convert a STM32CubeMX-generated Atollic Eclipse C project to C++ (3 steps):

  • Copy main.c to main.cpp and 'exclude' main.c from build. Result: Doesn't quite work yet. Although main.cpp might appear in the Eclipse project file list, it doesn't even begin to compile (won't catch obvious syntax errors) and there are linker errors for a missing main().
  • Next, Add org.eclipse.cdt.core.ccnature to the .project file with a text editor, like this... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> See http://www.openstm32.org/forumthread1244. This can be done while Eclipse is running with the project open. Result: main.cpp compiles but it can't find include files because the include paths are wrong. You'd think cppnature would have been a better name. Edit: search Eclipse help for "C++ nature" for tips on how to add C++ nature using menu File / New / Other / Convert to C/C++ Make Project.
  • Next, Modify project settings to duplicate pertinent C settings over to C++ settings - as shown here. enter image description here The project needs to be closed and re-opened for this change to take effect. Result: builds, links, runs and runs correctly. main.cpp can make calls to HAL c code and HAL callbacks can call functions in .cpp files. An extern "C" modifier may be needed in .cpp files if 1 the .cpp side is calling a function on the .c side (modify function prototypes by pre-pending defined extern "C" 2 .c side side is calling a callback defined in a .cpp (modify the function definition on the .cpp side by pre-pending defined extern "C".

Some notes:

  • If I ever re-generate code with CubeMX (e.g., to change a clock or pin-configuration), main.c will get updated but main.cpp will not - so it's necessary to merge changes/diffs from the .c file over to the .cpp file.
  • How to be certain the C & C++ settings are equivalent: After a build, look at the Console window and compare the command-line invocation for gcc and g++ to make sure pertinent parameters (paths, -D etc) are identical.
  • It's possible (but risky) to edit the Eclipse .cproject XML file and replicate the C settings over to the C++ side (specific details omitted here - but close project in Eclipse before editing the .cproject file).
  • Regarding objections to CubeMX/HAL 'bloat': CubeMX generates code and projects very quickly. If a HAL API is slow - e.g., GPIO port-pin access - then simply replace the API call with a new user-function containing a simpler subset of what that bloated API does. I do this frequently in time-critical code.
  • I'm using CubeMX (4.14.0) and Atollic (v6.0.0 Lite). I didn't see the project option to convert to C++ in the Atollic Eclipse although I have seen that option in other Eclipse environments e.g., Xilinx Eclipse.

Room for improvement: There might be a CubeMX template that could be edited to make these changes more automatic but I don't know enough about CubeMX templates to make this hack more elegant.

I use CubeMX with IAR EWARM too and go through a similar (but slightly easier) process to convert to a .cpp project.

1
votes

there is very simple way to build a cpp project in stm32cubeide

  1. after making new stm32 project check the c++ box
  2. do the initialize
  3. compile the code in main folder
  4. press right click and select source folder
  5. put your cpp file in it
  6. for call this cpp functions in main.c in cpp file make a simple function like main2()
  7. that you call it from main.c now you can use cpp function through main2() function
-1
votes

Depending on which STM32 chip it is, it might be supported by mBed. The online mBed compiler allows exporting to a variety of IDEs. The new offline one (yotta) uses cmake which can export to some IDEs too. Unfortunately yotta only supports a couple of chips at the moment.