Skip to main content
arduo
Associate III
February 7, 2019
Question

Undefined reference to HAL_UART_Init

  • February 7, 2019
  • 6 replies
  • 10715 views

Anyone knows how to fix undefined reference to HAL_UART_Init,HAL_UART_receive

I included all necessary files for this but somehow it doesn't find any reference

#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_conf.h"
#include "usb_device.h"
#include "usbd_comp_if.h"
#include "usbd_comp.h"
#include "usb_device.h"
#include "stm32f4xx_hal_usart.h"
#include "stm32f4xx_hal_uart.h"
#include <stm32f4xx_hal_uart.c>
#include "usbd_comp_if.h"
#include "usbd_comp.h"
#include "main.h"
#include <stm32f4xx_hal.h>
#include <stm32_hal_legacy.h>
 
extern HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);

    This topic has been closed for replies.

    6 replies

    AvaTar
    Senior III
    February 7, 2019

    A common convention in C coding is to NEVER EVER # include *.c files, i.e. sources.

    Apart from that, check where the missing function is implemented, and if the regarding source file is built with the project. In this case, supposedly stm32f4xx_hal_uart.c.

    Including said source file would effectively create a second implementation, resulting in duplicate linker symbols.

    You don't use C++, do you ?

    arduo
    arduoAuthor
    Associate III
    February 7, 2019

    no i use c

    i removed the double implementation it still throws an error

    Pavel A.
    February 7, 2019

    Including the C file is probably a desperate attempt to get the function defined :)

    The unresolved reference likely occurs because HAL_UART_MODULE_ENABLED not defined:

    https://github.com/pavel-a/stm32f4_libs/blob/c4560650ddaea6ee33de171027d801a31109565a/STM32Cube_FW_F4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c#L165

    This normally should be defined in your stm32f4xx_hal_conf.h.

    It is created and updated by CubeMX. If you added the UART outside of the Cube, add also #define HAL_UART_MODULE_ENABLED in the .h

    -- pa

    arduo
    arduoAuthor
    Associate III
    February 7, 2019

    Hi :beaming_face_with_smiling_eyes:

    I defined it thanks for the hint but it still isn't defined

    Piranha
    Principal III
    February 7, 2019

    Look from where comes the error - compiler or linker. Undefined reference is a linker error and means that compiled function can't be found in object files. In this case it means that stm32f4xx_hal_uart.c is not compiled and linked.

    AvaTar
    Senior III
    February 7, 2019

    Post the exact error message from your toolchain, not just short excerpts.

    And don't forget preceeding errors, if any.

    As a general rule, always start fixing with the first compiler error. Small syntactical errors can cause a torrent of aftereffects.

    Do a file search in *.c files for the HAL_UART_Init label.

    One of the files should supposedly be part of your project.

    If so, check for surrounding # if / # ifdef labels, that possible keep your compiler from seeing it.

    Know your tools.

    Tesla DeLorean
    Guru
    February 7, 2019

    ST does this differently, you include one top level include file, and it then pulls the stm32f4xx_hal_conf.h where you've marked the specific modules you want to include. And then you add the .C files from the library into the project.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    arduo
    arduoAuthor
    Associate III
    February 7, 2019

    thanks I fixed it that way.

    Now i got my composite device working any idea why i can't connect to USB Serial Device COM Port?

    arduo
    arduoAuthor
    Associate III
    February 7, 2019

    0690X000006DRGmQAO.jpg

    JTelö.1
    Visitor II
    December 16, 2020

    Hi,

    I have the same problem!

    I imported an example from the STM32Cube_FW_L0_V1.12.0 package, in this case a simple example of external interruption. So I have changed and I am trying to communicate using UART2. When I use the HAL_UART_Init () function, I'm getting the error: undefined reference to `HAL_UART_Init ' As already commented in this post by Clive1, I have already enabled the HAL_USART_MODULE_ENABLED module on stm32l0xx_hal_conf.h .

    Other modules (.c) that are in the same folder of the Library are compiled but stm32l0xx_hal_uart.c is not being found, but is in the linked folder. Could someone give me a tip?

    arm-none-eabi-gcc "D:/Desenvolvimento/Desenvto_software/STM32/STM32Cube_FW_L0_V1.12.0/Projects/NUCLEO-L073RZ/Examples/GPIO/GPIO_EXTI/Src/system_stm32l0xx.c" -mcpu=cortex-m0plus -std=gnu11 -g3 -DSTM32L073xx -DUSE_STM32L0XX_NUCLEO -DUSE_HAL_DRIVER -c -I../../../Inc -I../../../../../../../../Drivers/CMSIS/Device/ST/STM32L0xx/Include -I../../../../../../../../Drivers/STM32L0xx_HAL_Driver/Inc -I../../../../../../../../Drivers/BSP/STM32L0xx_Nucleo -I../../../../../../../../Drivers/CMSIS/Include -Os -ffunction-sections -Wall -fstack-usage -MMD -MP -MF"Drivers/CMSIS/system_stm32l0xx.d" -MT"Drivers/CMSIS/system_stm32l0xx.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Drivers/CMSIS/system_stm32l0xx.o"

    arm-none-eabi-gcc "D:/Desenvolvimento/Desenvto_software/STM32/STM32Cube_FW_L0_V1.12.0/Drivers/BSP/STM32L0xx_Nucleo/stm32l0xx_nucleo.c" -mcpu=cortex-m0plus -std=gnu11 -g3 -DSTM32L073xx -DUSE_STM32L0XX_NUCLEO -DUSE_HAL_DRIVER -c -I../../../Inc -I../../../../../../../../Drivers/CMSIS/Device/ST/STM32L0xx/Include -I../../../../../../../../Drivers/STM32L0xx_HAL_Driver/Inc -I../../../../../../../../Drivers/BSP/STM32L0xx_Nucleo -I../../../../../../../../Drivers/CMSIS/Include -Os -ffunction-sections -Wall -fstack-usage -MMD -MP -MF"Drivers/BSP/STM32L0xx_Nucleo/stm32l0xx_nucleo.d" -MT"Drivers/BSP/STM32L0xx_Nucleo/stm32l0xx_nucleo.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Drivers/BSP/STM32L0xx_Nucleo/stm32l0xx_nucleo.o"

    arm-none-eabi-gcc -o "STM32L073RZ_NUCLEO.elf" @"objects.list"  -mcpu=cortex-m0plus -T"../STM32L073RZTx_FLASH.ld" --specs=nosys.specs -Wl,-Map="STM32L073RZ_NUCLEO_GPIO_EXTI.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -Wl,--end-group

    Example/User/main.o: In function `MX_USART2_UART_Init':

    D:/Desenvolvimento/Desenvto_software/STM32/STM32Cube_FW_L0_V1.12.0/Projects/NUCLEO-L073RZ/Examples/GPIO/GPIO_EXTI/Src/main.c:168: undefined reference to `HAL_UART_Init'

    collect2.exe: error: ld returned 1 exit status

    make: *** [makefile:60: STM32L073RZ_NUCLEO.elf] Error 1

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

    21:59:32 Build Failed. 2 errors, 0 warnings. (took 6s.571ms)

    JNah.1
    Visitor II
    February 16, 2021

    you right, I have same problem.

    I use STM32CubeIDE 1.5.1 and HAL Library. If I create project and generate code with "Copy only necessary library files" option, it works well.

    But generate with "Add necessary library files as reference in the toolchain project configuration file" option, the error has occurred.

    I put the mouse cursor with Error function name (HAL_GPIO_WritePin, HAL_GPIO_Init, HAL_Init... and so on) and press function key 'F3', then

    IDE show me the library header file, and press F3 one more, then IDE not show the source code file. I confirm path of library files, and linking status, but I didn't see anything strange. I hope this problem is resolved.