[英]GCC weak attribute on constant variables
I have a question regarding weak attribute of const variable.我有一个关于 const 变量的弱属性的问题。 I have the following couple of files compiled with gcc:我有以下几个用 gcc 编译的文件:
main.c:主文件:
#include <stdio.h>
const int my_var __attribute__((weak)) = 100;
int
main(int argc, char *argv[])
{
printf("my_var = %d\n", my_var);
}
other.c:其他.c:
const int my_var = 200;
When I compile these two files and run the application I get the following result:当我编译这两个文件并运行应用程序时,我得到以下结果:
my_var = 100
Since I'm using weak attribute on the my_var
variable in main.c I thought it should be overridden by my_var
variable in other.c
, but that wasn't the case...由于我使用的是弱属性my_var
main.c中的变量我认为它应该是被覆盖由my_var
变量other.c
,但事实并非如此......
Now if I drop the const
keyword of my_var
in main.c
:现在,如果我在main.c
中my_var
的const
关键字:
#include <stdio.h>
/* Dropping const... */
int my_var __attribute__((weak)) = 100;
int
main(int argc, char *argv[])
{
printf("my_var = %d\n", my_var);
}
Then re-compile, I get the desired result:然后重新编译,得到想要的结果:
my_var = 200
Which is what I expect.这是我所期望的。
Note : If I drop the const
in the file other.c
I still get the result of 200.注意:如果我将const
放在文件other.c
我仍然会得到 200 的结果。
My question is: why using const
keyword changes the behaviour of weak
attribute?我的问题是:为什么使用const
关键字会改变weak
属性的行为? Is it related to which section the variable resides in?它与变量所在的部分有关吗?
The Makefile I'm using is:我正在使用的 Makefile 是:
.PHONY: all clean
TARGET=test
OBJS=main.o other.o
all: $(TARGET)
$(TARGET): $(OBJS)
gcc $(OBJS) -o $(TARGET)
main.o:main.c
gcc -c main.c
other.o:other.c
gcc -c other.c
clean:
rm -rf *.o $(TARGET)
Thanks in advance,提前致谢,
I faced the same issue (cf. GCC optimization bug on weak const variable ) and came up with the conclusion that gcc
does not handle well the optimization on the weak
/ const
variable within the file where the weak definition is: in your 'main.c' file, my_var
is always resolved to 100
.我面临着同样的问题(参见弱常量变量GCC优化错误),并得出结论,提出了gcc
没有处理好对优化weak
/ const
凡弱定义在文件中的变量:在你的“主。 c' 文件, my_var
总是解析为100
。
Not sure, but it seems to be a gcc
bug (?).不确定,但它似乎是一个gcc
错误(?)。
To solve this, you can :要解决此问题,您可以:
-O0
option to gcc
.通过向gcc
添加-O0
选项来防止优化。
-O0
should be the default value, so this might not help... -O0
应该是默认值,所以这可能无济于事......const
keyword as you did: gcc
cannot optimize no more as my_var
can potentially change now.像你一样去掉const
关键字: gcc
不能再优化了,因为my_var
现在可能会改变。const int my_var __attribute__((weak)) = 100;
设置你的const int my_var __attribute__((weak)) = 100;
statement in a separate source file ('main_weak.c' for instance): while building 'main.c', gcc
does not know the my_var
value and thus won't optimize.单独源文件中的语句(例如“main_weak.c”):在构建“main.c”时, gcc
不知道my_var
值,因此不会优化。The identifiers have internal linkage.标识符具有内部链接。 This is an obsolecense feature ( static
storage class specifier should be used).这是一个过时的特性(应该使用static
存储类说明符)。 In general, you should have a header with an extern
declaration of the identifier:通常,您应该有一个带有标识符的extern
声明的标头:
extern const int my_var;
Linking with different qualifiers is a bad idea.与不同的限定符链接是一个坏主意。
Maybe the better understandable approach is to use wekref
plus alias
.也许更好理解的方法是使用wekref
加上alias
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.