请教KEIL MDK 中 __weak 字使用?

2020-01-10 19:45发布

STM32CubeMX 生成的代码中 许多函数被定义为  __weak void HAL_xxxxx(X_HandleTypeDef *S)
如 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
在IRQHandler中断中被调用,请问应该如何正确使用此函数?
耐心等待诸位指点
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
13条回答
codeman
1楼-- · 2020-01-10 19:47
 精彩回答 2  元偷偷看……
codeman
2楼-- · 2020-01-10 23:24
下沉太快了,没人解惑,自己顶一下
codeman
3楼-- · 2020-01-10 23:33
This keyword instructs the compiler to export symbols weakly.

The __weak keyword can be applied to function and variable declarations, and to function definitions.
Usage
Functions and variable declarations
For declarations, this storage class specifies an extern object declaration that, even if not present, does not cause the linker to fault an unresolved reference.
For example:
__weak void f(void);
...
f(); // call f weakly
If the reference to a missing weak function is made from code that compiles to a branch or branch link instruction, then either:
The reference is resolved as branching to the next instruction. This effectively makes the branch a NOP.
The branch is replaced by a NOP instruction.
Function definitions
Functions defined with __weak export their symbols weakly. A weakly defined function behaves like a normally defined function unless a nonweakly defined function of the same name is linked into the same image. If both a nonweakly defined function and a weakly defined function exist in the same image then all calls to the function resolve to call the nonweak function. If multiple weak definitions are available, the linker generates an error message, unless the linker option --muldefweak is used. In this case, the linker chooses one for use by all calls.
Functions declared with __weak and then defined without __weak behave as nonweak functions.
Restrictions
There are restrictions when you qualify function and variable declarations, and function definitions, with __weak.
Functions and variable declarations
A function or variable cannot be used both weakly and nonweakly in the same compilation. For example, the following code uses f() weakly from g() and h():
void f(void);
void g()
{
    f();
}
__weak void f(void);
void h()
{
    f();
}
It is not possible to use a function or variable weakly from the same compilation that defines the function or variable. The following code uses f() nonweakly from h():
__weak void f(void);
void h()
{
    f();
}
void f() {}
The linker does not load the function or variable from a library unless another compilation uses the function or variable nonweakly. If the reference remains unresolved, its value is assumed to be NULL. Unresolved references, however, are not NULL if the reference is from code to a position-independent section or to a missing __weak function.
Function definitions
Weakly defined functions cannot be inlined.
Examples
__weak const int c;                // assume 'c' is not present in final link
const int *f1() { return &c; } // '&c' returns non-NULL if
                                   // compiled and linked /ropi
__weak int i;                      // assume 'i' is not present in final link
int *f2() { return &i; }       // '&i' returns non-NULL if
                                   // compiled and linked /rwpi
__weak void f(void);               // assume 'f' is not present in final link
typedef void (*FP)(void);
FP g() { return f; }               // 'g' returns non-NULL if
                                   // compiled and linked /ropi

英文不好,麻烦大侠通俗解释下,先谢谢了
大豆皮
4楼-- · 2020-01-11 03:02
是哑函数吧。用户可以自己写同名函数,然后编译器会用用户的。
chenhaimeng123
5楼-- · 2020-01-11 07:23
lz我在看ST官方的STM32CubeExpansion_SPN7_V1.0.0代码;
直接这个函数是空的,难道是没用到?
  1. /**
  2.   * @brief  Hall commutation changed callback in non blocking mode
  3.   * @param  htim : TIM handle
  4.   * @retval None
  5.   */
  6. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  7. {
  8.   /* NOTE : This function Should not be modified, when the callback is needed,
  9.             the HAL_TIMEx_CommutationCallback could be implemented in the user file
  10.    */
  11. }
复制代码
error_dan
6楼-- · 2020-01-11 07:40
 精彩回答 2  元偷偷看……

一周热门 更多>