什么是弱函数及其用途?我正在使用STM32F429微控制器 [英] What are weak functions and what are their uses? I am using a stm32f429 micro controller

查看:1202
本文介绍了什么是弱函数及其用途?我正在使用STM32F429微控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科说:

弱符号表示在链接的过程中经过特殊注释的符号 可执行和可链接格式(ELF)对象文件.默认情况下,不带 任何注释,目标文件中的符号都是很强的.在链接期间, 强符号可以覆盖同名的弱符号.在 相反,两个共用一个名称的强符号会产生链接错误 在链接时.链接二进制可执行文件时,弱声明 符号不需要定义.相比之下,(默认情况下)a 没有定义的声明的强符号将触发未定义 符号链接错误. C或C ++语言未提及弱符号 标准;因此,将它们插入代码不是很方便. 即使两个平台支持相同或相似的标记语法 由于符号较弱,语义可能会有所不同,例如 运行时动态链接期间的弱符号是否丢失 是否语义.

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.

弱函数是什么?它们的用途是什么?我正在使用stm32f429微控制器.库中有一些弱函数.但是我不明白它们的用途以及它们的用途!

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!

我在Google上进行了搜索,但没有得到满意的答案.

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

推荐答案

当函数以描述符__weak开头时,基本上意味着如果(编码人员)未定义它,则在此处定义它.

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.

让我们看看我的主要敌人"HAL_UART_RxCpltCallback()".

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

此功能存在于STM32F4-HAL代码库的HAL中,您可以从ST-Micro下载该功能.

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

在文件stm32f4xx_hal_uart.c中,您会发现此函数定义为:

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
  */
}

因此,如此处代码中的注释所述,请将此函数放置在您自己的用户文件中.但是,执行此操作时,请勿输入__weak项.这意味着链接器将使用您对HAL_UART_RxCpltCallback()函数的定义,而不是stm32f4xx_hal_uart.c文件中定义的函数.

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.

这使通用代码库能够始终进行编译.您不必编写一堆您不感兴趣的函数,但是它们可以编译.编写自己的脚本时,您不必将自己定义为__weak并编写它.

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.

简单吗?有帮助吗?

干杯!

这篇关于什么是弱函数及其用途?我正在使用STM32F429微控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆