我们使用 gcc4.1.2 构建了代码,并且使用了在“sys/stat.h”系统头文件中定义的函数“lstat64”,并且也在我们使用的第三方库中定义了该函数。
当我们“nm”我们的可执行文件时,我们发现:
W lstat64
我的问题是:为什么 gcc 将其标记为弱函数?
另外,我们已经将代码移植到了gcc4.4.4,我们发现新的gcc并没有将函数标记为“弱”,而是将其标记为未定义?
为什么会出现这种行为变化?
最佳答案
根据 GCC documentation :
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. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.
在您的情况下,lstat64
可能在 GCC 4.1.2 中被标记为弱,因为这样它就不会与第三方库函数冲突。 GCC 可能希望这些外部函数具有优先权。
但在更高版本中,GCC 会希望自己的 lstat64
版本具有优先权。
关于c - 为什么 gcc 可以自动将符号标记为弱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55139165/