Failed to build STM32CubeMX project with error: In function `LoopFillZerobss': (.text.Reset_Handler+0x32): undefined reference to `main'

Hi,

I bought the P-NUCLEO-IHM03 development kit and wish to use vscode and PlatformIO to build and flash. I followed the official instruction to generate the motor control code by STM32CubeMX (Src, Inc, etc), and tried to build (compile) the project with PlatformIO but failed.

The error is:

Archiving .pio\build\nucleo_g431rb\libFrameworkCMSISDevice.a
Linking .pio\build\nucleo_g431rb\firmware.elf
.pio\build\nucleo_g431rb\libFrameworkCMSISDevice.a(startup_stm32g431xx.o): In function LoopFillZerobss': (.text.Reset_Handler+0x32): undefined reference to main’
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nucleo_g431rb\firmware.elf] Error 1

Does anyone have a clue on how to solve this issue? Please let me know if you need more info to identify the problem. I appreciate your discussion and help!

More info about the kit:
Control board: STM32 Nucleo G431RB
Power board: X-NUCLEO-IHM16M1

My working environment is windows 11.
Here is the main.c file generated by CubeMX:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "cordic.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_DMA_Init();
  MX_ADC1_Init();
  MX_ADC2_Init();
  MX_CORDIC_Init();
  MX_TIM1_Init();
  MX_USART2_UART_Init();
  MX_MotorControl_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;
  RCC_OscInitStruct.PLL.PLLN = 85;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enables the Clock Security System
  */
  HAL_RCC_EnableCSS();
}

/**
  * @brief NVIC Configuration.
  * @retval None
  */
static void MX_NVIC_Init(void)
{
  /* USART2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART2_IRQn, 3, 1);
  HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* DMA1_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  /* TIM1_BRK_TIM15_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(TIM1_BRK_TIM15_IRQn, 4, 1);
  HAL_NVIC_EnableIRQ(TIM1_BRK_TIM15_IRQn);
  /* TIM1_UP_TIM16_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
  /* ADC1_2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(ADC1_2_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
  /* EXTI15_10_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Not sure if you need the full log so I paste it here:

Processing nucleo_g431rb (platform: ststm32; board: nucleo_g431rb; framework: stm32cube)
Verbose mode can be enabled via -v, --verbose option
CONFIGURATION:

https://docs.platformio.org/page/boards/ststm32/nucleo_g431rb.html

PLATFORM: ST STM32 (17.3.0) > Nucleo G431RB
HARDWARE: STM32G431RBT6 170MHz, 32KB RAM, 128KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES:

  • framework-stm32cubeg4 @ 1.4.0
  • tool-ldscripts-ststm32 @ 0.2.0
  • toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
    LDF: Library Dependency Finder → https://bit.ly/configure-pio-ldf
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 23 compatible libraries
    Scanning dependencies…
    No dependencies
    Building in release mode
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_adc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_adc_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_comp.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_cordic.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_cortex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_crc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_crc_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_cryp.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_cryp_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_dac.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_dac_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_dma.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_dma_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_exti.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_fdcan.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_flash.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_flash_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_flash_ramfunc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_fmac.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_gpio.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_hrtim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_i2c.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_i2c_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_i2s.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_irda.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_iwdg.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_lptim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_nand.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_nor.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_opamp.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_opamp_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_pcd.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_pcd_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_pwr.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_pwr_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_qspi.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_rcc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_rcc_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_rng.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_rtc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_rtc_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_sai.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_sai_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_smartcard.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_smartcard_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_smbus.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_smbus_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_spi.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_spi_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_sram.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_tim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_tim_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_uart.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_uart_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_usart.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_usart_ex.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_hal_wwdg.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_adc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_comp.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_cordic.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_crc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_crs.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_dac.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_dma.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_exti.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_fmac.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_fmc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_gpio.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_hrtim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_i2c.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_lptim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_lpuart.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_opamp.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_pwr.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_rcc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_rng.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_rtc.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_spi.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_tim.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_ucpd.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_usart.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_usb.o
    Compiling .pio\build\nucleo_g431rb\FrameworkHALDriver\Src\stm32g4xx_ll_utils.o
    Compiling .pio\build\nucleo_g431rb\FrameworkCMSISDevice\gcc\startup_stm32g431xx.o
    Compiling .pio\build\nucleo_g431rb\FrameworkCMSISDevice\system_stm32g4xx.o
    Archiving .pio\build\nucleo_g431rb\libFrameworkCMSISDevice.a
    Linking .pio\build\nucleo_g431rb\firmware.elf
    .pio\build\nucleo_g431rb\libFrameworkCMSISDevice.a(startup_stm32g431xx.o): In function LoopFillZerobss': (.text.Reset_Handler+0x32): undefined reference to main’
    collect2.exe: error: ld returned 1 exit status
    [.pio\build\nucleo_g431rb\firmware.elf] Error 1

You’re not converting the STM32CubeIDE project correctly at all.

include_dir is the name of the folder that you want to set as include directory. Not some build flag. So if you want it to be Inc, it’s include_dir = Inc, not include_dir = -I Inc. Same goes for src_dir. See documentation.

Next I see you still have “Drivers” etc. in the root of the project – if that contains the STM32HAL, CMSIS etc., then setting framework = stm32cube is wrong because then PlatformIO will try to pull in its own version of the STM32HAL, etc. Also only sources in src/ or lib/<subfolder> will be compiled, a folder like Drivers at the root of the project will be completely ignored.

Please use stm32pio as conversion program or see related topics like How to write build_flags in platformio.ini.

@zy1191231587
Have you resolved this problem?
I got the same error message today, do you know the reason now?
my platformio.ini as below:

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = stm32cube

build_src_filter =
  +USB_DEVICE/APP/usb_device.c
  
build_flags = 
  -IUSB_DEVICE/APP/
  -IUSB_DEVICE/Target/
  -IC:/Users/Ssd/STM32Cube/Repository/STM32Cube_FW_F1_V1.8.4/Middlewares/ST/STM32_USB_Device_Library/Core/Inc
  -IC:/Users/Ssd/STM32Cube/Repository/STM32Cube_FW_F1_V1.8.4/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc

@maxgerhardt
I have the same question. Is my platformio.ini incorrect also?

This isn’t the right syntax for that, it’s missing the <> around the source file at least (docs).

The -I flags also indicate that you’re trying to include sources from a whole other directory outside the project. Something is majorly wrong here. Please try and use https://github.com/ussserrr/stm32pio or use the stm32cube reference examples at https://github.com/platformio/platform-ststm32/tree/develop/examples.