Undefined reference to a function

when I compile my stm32 code, there are some questions that undefined reference to some

Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
PLATFORM: ST STM32 (14.1.0) > STM32F103VE (64k RAM. 512k Flash)
HARDWARE: STM32F103VET6 72MHz, 64KB RAM, 512KB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink, stlink)

  • framework-cmsis 2.50501.200527 (5.5.1)
  • framework-cmsis-stm32f1 4.3.1
  • tool-ldscripts-ststm32 0.1.0
  • toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
    LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 1 compatible libraries
    Scanning dependencies…
    No dependencies
    Building in release mode
    Compiling .pio/build/genericSTM32F103VE/FrameworkCMSIS/gcc/startup_stm32f103xe.o
    Compiling .pio/build/genericSTM32F103VE/FrameworkCMSIS/system_stm32f1xx.o
    Compiling .pio/build/genericSTM32F103VE/src/main.o
    Compiling .pio/build/genericSTM32F103VE/src/stm32f10x_it.o
    Linking .pio/build/genericSTM32F103VE/firmware.elf
    .pio/build/genericSTM32F103VE/src/main.o: In function LED_GPIO_Config': main.c:(.text.LED_GPIO_Config+0x6): undefined reference to RCC_APB2PeriphClockCmd’
    main.c:(.text.LED_GPIO_Config+0x26): undefined reference to GPIO_Init' main.c:(.text.LED_GPIO_Config+0x32): undefined reference to GPIO_Init’
    main.c:(.text.LED_GPIO_Config+0x3e): undefined reference to GPIO_Init' main.c:(.text.LED_GPIO_Config+0x46): undefined reference to GPIO_SetBits’
    main.c:(.text.LED_GPIO_Config+0x4e): undefined reference to GPIO_SetBits' main.c:(.text.LED_GPIO_Config+0x56): undefined reference to GPIO_SetBits’
    collect2: error: ld returned 1 exit status
    *** [.pio/build/genericSTM32F103VE/firmware.elf] Error 1

This is my platformio.ini

I design header and library file like following picture

How can I solve this error?

Try renaming your *.c source files to *.cpp. I suspect that due to C++ name mangling, and that the libraries are in C++, that the calls from your C code to the C++ library functions are failing.

As far as I remember, C has extreme difficulties calling C++ code, while C++ can call C code, provided that the C code is declared, to the C++ compiler, to be extern C.

I think this might solve your problem.



You can’t put the sources directly in lib/, they need to be in a subfolder. Refer to the README.

1 Like