STM CubeMX and multiple definition errors

So I’m using the latest STM CubeMX program version 4.23.0 to bootstrap the code of a STM32F103 based blue pill board, using Makefiles as the Toolchain/IDE.

After generating the project and executing the make command at the project root directory, I have the following errors:

build/main.o: In function `_Error_Handler':
/opt/ARM/Projects/TTNBluePill/Src/main.c:259: multiple definition of `_Error_Handler'
build/main.o:/opt/ARM/Projects/TTNBluePill/Src/main.c:259: first defined here
build/main.o: In function `MX_GPIO_Init':
/opt/ARM/Projects/TTNBluePill/Src/main.c:240: multiple definition of `SystemClock_Config'
build/main.o:/opt/ARM/Projects/TTNBluePill/Src/main.c:240: first defined here
build/main.o: In function `MX_GPIO_Init':
/opt/ARM/Projects/TTNBluePill/Src/main.c:240: multiple definition of `main'
build/main.o:/opt/ARM/Projects/TTNBluePill/Src/main.c:240: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: multiple definition of `NMI_Handler'
build/stm32f1xx_it.o:/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: multiple definition of `HardFault_Handler'
build/stm32f1xx_it.o:/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: multiple definition of `MemManage_Handler'
build/stm32f1xx_it.o:/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: multiple definition of `BusFault_Handler'
build/stm32f1xx_it.o:/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: multiple definition of `UsageFault_Handler'
build/stm32f1xx_it.o:/opt/ARM/Projects/TTNBluePill/Src/stm32f1xx_it.c:52: first defined here
build/stm32f1xx_it.o: In function `NMI_Handler':
....
....
....

And the code linkage fails.

The issue is that the generated CubeMX Makefile has duplicated entries:

######################################
# source
######################################
# C sources
C_SOURCES =  \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \

Src/main.c \

Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \
Src/system_stm32f1xx.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi_ex.c \

Src/stm32f1xx_it.c \
Src/stm32f1xx_hal_msp.c \

Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \

Src/main.c \
Src/stm32f1xx_it.c \

Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \

Src/stm32f1xx_hal_msp.c \

Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \
Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c  

We can see that main.c, stm32f1xx_hal_msp.c and stm32f1xx_it.c are included twice on the C_SOURCES entry. For sure a CubeMX bug.

Anyway, just delete the duplicated entries, and now the compilation and linkage succeeds.

Happy coding!

Advertisements

7 thoughts on “STM CubeMX and multiple definition errors

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.