gcc: pragma: weak

6.62.11 Weak Pragmas ; 为了和SRV4的兼容。
For compatibility with SVR4, GCC supports a set of #pragma directives for declaring symbols to be weak, and defining weak aliases.
#pragma weak symbol
This pragma declares symbol to be weak, as if the declaration had the attribute of the same name. The pragma may appear before or after the declaration of symbol. It is not an error for symbol to never be defined at all.
定义了一个弱符号。这个pragma weak可以出现符号声明的前面或者后面,对位置,没有要求。这样即使没有定义这个符号,也不会报错误。
#pragma weak symbol1 = symbol2
This pragma declares symbol1 to be a weak alias of symbol2. It is an error if symbol2 is not defined in the current translation unit.;这个是说symbol1是一个弱链接到symbol2。如果symbol2没有定义,就会出错。

这个使用的情景是,在特定的环境下再定义一个函数来进行调用。在其他情况下不需要执行的时候。算是一种特殊的处理。编译即使找不到这个符号,也没有关系。当然要判断符号是否存在。就是是取地址符号。

猜你喜欢

转载自blog.csdn.net/qq_36428903/article/details/131894915
gcc