它看起来像 GCC,-O2
并__attribute__((weak))
根据您引用弱符号的方式产生不同的结果。考虑一下:
$猫弱.c
#include <stdio.h>
extern const int weaksym1;
const int weaksym1 __attribute__(( weak )) = 0;
extern const int weaksym2;
const int weaksym2 __attribute__(( weak )) = 0;
extern int weaksym3;
int weaksym3 __attribute__(( weak )) = 0;
void testweak(void)
{
if ( weaksym1 == 0 )
{
printf( "0\n" );
}
else
{
printf( "1\n" );
}
printf( "%d\n", weaksym2 );
if ( weaksym3 == 0 )
{
printf( "0\n" );
}
else
{
printf( "1\n" );
}
}
$猫测试.c
extern const int weaksym1;
const int weaksym1 = 1;
extern const int weaksym2;
const int weaksym2 = 1;
extern int weaksym3;
int weaksym3 = 1;
extern void testweak(void);
void main(void)
{
testweak();
}
$ 制作
gcc -c weak.c
gcc -c test.c
gcc -o test test.o weak.o
$ ./测试
1
1
1
$ 使 ADD_FLAGS="-O2"
gcc -O2 -c weak.c
gcc -O2 -c test.c
gcc -O2 -o test test.o weak.o
$ ./测试
0
1
1
问题是,为什么最后一个“./test”产生“0 1 1”,而不是“1 1 1”?
gcc 版本 5.4.0 (GCC)