Skip to main content
jcRuan
Associate
May 20, 2020
Solved

Using STM32CubeMX generate code for STM32CubeIDE and than change main.c to main.cpp, and build project will show up undefined reference to 'main' error.

  • May 20, 2020
  • 6 replies
  • 6573 views

The error message like as:

Core/Startup/startup_stm32f765vitx.o: In function `LoopFillZerobss':

J:/STM32PRJ/CB_ENV/MS220_F765VI/VIDEOBOARD_V9A/20200520/Debug/../Core/Startup/startup_stm32f765vitx.s:97: undefined reference to `main'

collect2.exe: error: ld returned 1 exit status

make: *** [makefile:47: VideoBoard.elf] Error 1

"make -j8 all" terminated with exit code 2. Build might be incomplete.

How can I solve this problem?

This topic has been closed for replies.
Best answer by jcRuan

Hi @alister​ 

​Sorry, I misunderstood what you meant, but i solved this problem, thanks your help.

The setting steps is:

  1. Generate (or regenerate) code.
  2. Mouse click right on the project icon as: 0693W000001cgiFQAQ.png
  3. Select "Convert to C++" item as:0693W000001cgiKQAQ.png
  4. Change main.c to main.cpp( from STM32CubeIDE rename). Don't rename from the Windows file system manually. If regenerate code need to rename again and maybe need to porting old main.cpp to new main.cpp(last rename) again. 0693W000001cgiPQAQ.png
  5. Build project.

6 replies

alister
Senior III
May 20, 2020

Explained https://www.geeksforgeeks.org/extern-c-in-c/ and many other places.

The LoopFillZerobss was indented to call a "C" main. Declare main like this in main.c before you define it, i.e. before the main() function.

extern "C"
{ 
 int main(void); 
} 

jcRuan
jcRuanAuthor
Associate
May 21, 2020

Hi @alister​ 

Thanks your replay.

I try to put extern "c" before the main() function but it doesn't work, the error message is the same. the code snippet following:

extern "C"
{
	int main(void)
	{
	 /* USER CODE BEGIN 1 */
 
	 /* USER CODE END 1 */
	
	 /* MCU Configuration--------------------------------------------------------*/
	
	 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	 HAL_Init();
	
	 /* USER CODE BEGIN Init */
	
	 /* USER CODE END Init */
	
	 /* Configure the system clock */
	 SystemClock_Config();
	
	 /* USER CODE BEGIN SysInit */
	
	 /* USER CODE END SysInit */
	
	 /* Initialize all configured peripherals */
	 MX_GPIO_Init();
	 MX_I2C3_Init();
	 MX_SPI3_Init();
	 MX_TIM6_Init();
	 /* USER CODE BEGIN 2 */
	 //sys_main->SysMainInit();//this is cpp function
	 /* USER CODE END 2 */
	
	 /* Infinite loop */
	 /* USER CODE BEGIN WHILE */
	 while (1)
	 {
		 //sys_main->SysMainLoop();//this is cpp function
		/* USER CODE END WHILE */
	
		/* USER CODE BEGIN 3 */
	 }
	 /* USER CODE END 3 */
	}
}

Do I need to modify the compilation settings?

alister
Senior III
May 21, 2020

I'm guessing, try declaring it only as extern "C", not defining it.

Is there a main with a C++ mangled name in the linker map file, or what does main look like there?

Are you sure you're compiling/linking the file main's in?

Check https://stackoverflow.com/questions/18137814/loopfillzerobss-has-an-undefined-reference-to-main too.