在GCC中,如果使用 weak 属性声明变量,并且在(静态)链接时间内未找到定义,则变量将具有地址零,即,如果使用变量的地址初始化指针,则指针将 NULL ,如下面的代码片段所示:
foobar.c但是:
extern int foo __attribute__((weak));
extern int bar;
int *a[] = {&foo, &bar};
main.c中:
#include <diag/Trace.h>
//int foo;
int bar;
extern int *a[];
int main(void) {
trace_printf("%p, %p", a[0], a[1]);
return 0;
}
输出为: 0,0x20000120 (我使用arm-none-eabi-gcc 5.4.1)
问题是:虽然行为是预期的,但没有文件提到它。有人能指出我解释这种行为的任何材料吗?谢谢!
答案 0 :(得分:0)
我并不认为我会这样,我假设GCC会做同样的事情,好像弱属性不存在一样。文档是:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
...所以这看起来像未记录/未定义的行为。