c - 为什么 gcc 可以自动将符号标记为弱

标签 c linux gcc weak-linking

我们使用 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/

相关文章:

c - 意外的 pipe() 行为

c - 寄存器 const 变量将存储在哪里?

c - 如何计算大 n 的 2^n?

linux - 为什么 proc_create 模式参数 0 对应于 0444

c - 复合文字的赋值有什么用?

c++ - Lua C API 内存泄漏? (valgrind)

php - 安装 MongoDB PHP 驱动程序(64 位 linux)

python - 在解析 JSON 文件数据时,根据 Python 中的配置文件中提到的位置添加具有空值的缺失字段

debugging - 如果支持,如何在 Makefile 中选择 -Og 而不是 -O0

c - GCC 在初始化程序周围缺少大括号