Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are weak functions and what are their uses? I am using a stm32f429 micro controller

Wikipedia says:

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. In contrast, two strong symbols that share a name yield a link error during link-time. When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error. Weak symbols are not mentioned by C or C++ language standards; as such, inserting them into code is not very portable. Even if two platforms support the same or similar syntax for marking symbols as weak, the semantics may differ in subtle points, e.g. whether weak symbols during dynamic linking at runtime lose their semantics or not.

What are the weak functions and what are their uses? I am using an stm32f429 micro controller. There are some weak functions in the library. But I can't understand, what they and their use!

I searched about it on google but did't get a satisfactory answer.

like image 666
Munir Ahmad Avatar asked Feb 19 '16 14:02

Munir Ahmad


People also ask

What are weak functions?

__weak function are methods that can be overwritten by user function with same name, used to define vector tables, and default handlers. Normal function writing (declaration and definition) are considered strong meaning that the function name cannot be re declared, you will get compiler/linker error.

What is strong and weak symbols in C?

For c program, if you define an global variable and not initialize it, GCC will regard it as weak symbol. However, for c++ program, the default type is strong variable. That is to say, for line int gvar; in main. cpp , it is a strong symbol. Since we have another strong symbol with the same name in aux.

What is __ Attribute__ weak ))?

__attribute__((weak)) variable attributeGenerates a weak symbol for a variable, rather than the default symbol. extern int foo __attribute__((weak)); At link time, strong symbols override weak symbols.


1 Answers

When a function is prepended with the descriptor __weak it basically means that if you (the coder) do not define it, it is defined here.

Let us take a look at my arch-nemesis "HAL_UART_RxCpltCallback()".

This function exists within the HAL of the STM32F4-HAL code base that you can download from ST-Micro.

Within the file stm32f4xx_hal_uart.c file you will find this function defined as:

__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  /* NOTE: This function Should not be modified, when the callback is needed,
       the HAL_UART_RxCpltCallback could be implemented in the user  file
  */
}

So, as the note within the code here says, place this function inside your own user files. However when you do that, do not put in the __weak term. This means that the linker will take your definition of the HAL_UART_RxCpltCallback() function and not the one defined within the stm32f4xx_hal_uart.c file.

This gives the generic code base the ability to always compile. You don't have to write a whole bunch of functions that you are not interested in but it will compile. When it comes time to writing your own, you just have to NOT define yours as __weak and write it.

Simple? Helpful?

Cheers!!

like image 90
Dowd Avatar answered Oct 01 '22 23:10

Dowd