Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atollic TrueSTUDIO: How to convert from C to C++?

I try to convert my project to C++ in TrueSTUDIO for STM32 by

  • Selecting project (in C/C++ Projects tab)
  • Right mouse button, select New/Other
  • In C/C++ tab, Convert to a C/C++ Project (Adds C/C++ Nature)
  • When pressing Next, nothing happens (unclear why)
  • When I press Next again, I see: Convert to a C/C++ project: The wizards adds C/C++ Nature to the selected projects to enable C/C++ Tools Supports for them
  • I press Finish

Than nothing happens, when I change main.c to main.cpp, I get the following error after build:

startup\startup_stm32f407xx.o: In function `LoopFillZerobss':
C:\Users\Michel\OneDrive\Stm32\Stm32CubeProjects\Fcb1010\Debug/..\startup/startup_stm32f407xx.s:115: undefined reference to `main'
collect2.exe: error: ld returned 1 exit status

It seems still C is used (also in the command line (first part):

arm-atollic-eabi-gcc -o Fcb1010.elf Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal.o Drivers\STM ...

I would expect g++ to be used.

How to change my settings/procedure to be able to use C++ within TrueSTUDIO for a CubeMX generated project?

UPDATE

I removed Atollic TrueStudio, removed AC6 System Workbench, and reinstalled AC6 System Workbench. Now I can use C++ at AC6 System Workbench, even using STL.

I do not dare to install Atollic TrueStudio again, since it breaks the existing installation of AC6 SystemWorkbench, probably because they both use Eclipse. A pity, since I liked some features of TrueStudio, but C++ is more important to me. So for me, no TrueStudio anymore.

like image 397
Michel Keijzers Avatar asked Mar 23 '18 22:03

Michel Keijzers


2 Answers

The error you mention

startup\startup_stm32f407xx.o: In function 'LoopFillZerobss': C:\Users\Michel\OneDrive\Stm32\Stm32CubeProjects\Fcb1010\Debug/..\startup/startup_stm32f407xx.s:115: undefined reference to `main' collect2.exe: error: ld returned 1 exit status

Is a linker problem, I suspect the library is looking for a c main function. A C++ compiler performs name mangling, so the symbol will no longer be main.

Try changing the signature of your main to extern "C" int main(void)

like image 168
Colin Avatar answered Oct 23 '22 20:10

Colin


Please follow below steps:-

  1. Better convert all the .c file to .cpp manually
  2. Check each header file and change macro which is specific to C language
  3. change the main function from C type to C++ (void main to int main()) also return a value from your main function.
  4. If you are using a make file check if specific C dependencies are referred and change them.

Hope this will help.

like image 32
Abhijit Pritam Dutta Avatar answered Oct 23 '22 20:10

Abhijit Pritam Dutta