cstm32halstm32cubemx

Using HAL libraries without uncommenting HAL_conf.h specific library


I want to use uart without uncommenting stm32****_HAL_conf.h:

#define HAL_UART_MODULE_ENABLED

I've tried to add it on main.h and/or including manually:

#include "stm32h7xx_hal_uart.h"

but these adding causes to cubeIDE to tell: Undefined reference to.

I wonder why uncommenting that define solve all errors (Note: thos only causes uart header to being included in HAL_conf.h) but customly adding them to main.h will giving us error. Since by every cubeMX refresh the HAL_conf.h resets to it's default. Also I've seen this usefull question undefined reference when including a header file .

It seems the defenition in the code changes linker behaviour, doesn't it? But how?

What I've done?

1.Added required file to the project uart header and source. 2.including the header file in program 3.defining the flag to let #ifdef's to doing their job But no work is done.


Solution

  • Each HAL driver's source file checks to see if its HAL_FOO_MODULE_ENABLED symbol is defined. If it is not, the driver still compiles and generates a .o file, but this will contain no functions. This will lead to linker errors if those driver functions are referenced in other code.

    Therefore, HAL_FOO_MODULE_ENABLED must be defined in a file that the driver #includes. The drivers do not #include "main.h", so you cannot add the definition there.

    CubeMX will automatically define symbols for used drivers in stm32xxx_hal_conf.h. Note that this is an autogenerated file, and will be overwritten anytime CubeMX deems it necessary to change anything. So while it is possible to manually edit that file, you'd need to be aware that it could revert later.

    There is probably not a safe place you can manually add these defines, that will not be reverted by CubeMX at a later time. They could probably be added to your compiler flags, e.g. -DHAL_UART_MODULE_ENABLED if you really needed to do it this way.