5
\$\begingroup\$

I have started learning STM32 programming in HAL and I am confused about what this function exactly does:

__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  /* Prevent unused argument(s) compilation warning */
  UNUSED(GPIO_Pin);
  /* NOTE: This function should not be modified, when the
  callback is needed, the HAL_GPIO_EXTI_Callback could be
  implemented in the user file
  */
}

What is the significance of the keyword (I am not sure if it's actually a keyword) __weak here and where do you use it? I saw a video where the lecturer said something about it but I did not understand.

\$\endgroup\$

3 Answers 3

11
\$\begingroup\$

The __weak keyword means that the function can be overridden by creating another function with the same declaration.

Many of the interrupt-functions etc. in the STM HAL libraries are declared as weak so that you can override them with your own function, instead of modifying the library functions.

From GCC Manual:

weak
The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations.

\$\endgroup\$
3
  • \$\begingroup\$ So it's the moral equivalent of a C++ virtual class method, sounds like? "Insert implementation here." (But unlike virtual methods, the user (or someone) isn't required to supply an implementation. So, somewhere between a non-final method that can be replaced with an override, and a virtual placeholder that needs to be replaced with an implementation.) \$\endgroup\$
    – FeRD
    Dec 31, 2021 at 23:46
  • 2
    \$\begingroup\$ @FeRD sort of yeah, except it's just linker magic. Normally if there are two definitions of a symbol you get an error. But if one is weak and one isn't, the "strong" definition wins, with no complaint. \$\endgroup\$
    – hobbs
    Jan 1, 2022 at 3:10
  • 1
    \$\begingroup\$ The function is not overwritten. The linker is choosing this function or function with the same name but having a strong linkage. It is done during the linking stage and the compiler is not involved here \$\endgroup\$ Jan 2, 2022 at 0:13
8
\$\begingroup\$

As explained in this stackexchange question a function defined as "_weak" can be overwritten by a user-defined function with the same name.
It basically is a default function. If you don't write your own, the compiler will use the weak one. If you do define your own function with the same name, the compiler will ignore the weak one.

\$\endgroup\$
5
\$\begingroup\$

It has nothing to do with STM32 or embedded programming.

It's just a compiler dependent extension to tell the C compiler that an object is weakly declared, as by default objects are strongly declared.

The example posted is just a way for the HAL to provide a default implementation of a function if the user does not write a function with the same name to override the default weak implementation.

\$\endgroup\$
5
  • \$\begingroup\$ instead of adding things to the function and redefining it, can we not just pass arguments to the function as per our application? \$\endgroup\$ Dec 31, 2021 at 10:51
  • 1
    \$\begingroup\$ I don't follow what you ask. The HAL code requires that there is a function so it can be compiled. So if you use HAL and don't provide this function, you can't compile it due to missing function. That is why the HAL provides a default function so it can be compiled without you providing a function even if you don't need it, and if you need it then provide the function yourself. \$\endgroup\$
    – Justme
    Dec 31, 2021 at 10:58
  • \$\begingroup\$ here is what I understood: we can make changes or make additions to the function defined with the keyword "__weak" . But when we want to use the modified version of that function, we should not use the keyword "__weak", it tells the compiler "hey, if you find the function with same name, compile the one which does not have the keyword "__weak". Have I understood it right? \$\endgroup\$ Dec 31, 2021 at 11:02
  • 5
    \$\begingroup\$ Have you read the HAL user manual? Have you read the NOTE in the code you quoted? The philosophy is that you provide the function, and you are supposed to use the HAL as given, instead of making changes into HAL files. \$\endgroup\$
    – Justme
    Dec 31, 2021 at 11:10
  • 1
    \$\begingroup\$ @user8737703 no, you're making it too complicated. There is no "modified version of the function". You just define a callback if you need one, or you don't if you don't. \$\endgroup\$
    – hobbs
    Jan 1, 2022 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.