两个具有相同名称和类型的变量,在两个不同的.c文件中,使用gcc进行编译

时间:2020-12-14 16:50:12

Here's the deal. I've had two identical global variables in two different .c files, they weren't declared as extern. So each .c file should have seen its own variable, right?

这是交易。我在两个不同的.c文件中有两个相同的全局变量,它们没有被声明为extern。所以每个.c文件都应该看到自己的变量,对吧?

But I have gotten some really strange behaviour, as if one file was reading the other files variable (after linking them together). Adding 'static' qualifier to both variables definitions seemed to fix this issue.

但是我得到了一些非常奇怪的行为,好像一个文件正在读取其他文件变量(将它们链接在一起之后)。将“静态”限定符添加到两个变量定义似乎解决了这个问题。

So what I'm actually wondering is, what exactly happened there without the 'static' qualifier?

所以我真正想知道的是,在没有'静态'限定符的情况下究竟发生了什么?

4 个解决方案

#1


18  

So each .c file should have seen its own variable, right?

所以每个.c文件都应该看到自己的变量,对吧?

Wrong. In C, omitting static from a declaration means implicit extern linkage.

错误。在C中,省略声明中的静态意味着隐式的外部链接。

From C In a Nutshell:

来自C in a Nutshell:

The compiler treats function declarations without a storage class specifier as if they included the specifier extern. Similarly, any object identifiers that you declare outside all functions and without a storage class specifier have external linkage.

编译器在没有存储类说明符的情况下处理函数声明,就像它们包含说明符extern一样。类似地,您在所有函数外部声明但没有存储类说明符的任何对象标识符都具有外部链接。

#2


4  

ALWAYS initialize global variable, then compiler will not consider its linkage automatically as extern. Compiler will throw error during compilation.

总是初始化全局变量,然后编译器不会自动将其链接视为extern。编译器将在编译期间抛出错误。

This will help to avoid random problems in big code base ,because our code may use somebody else declared variable that has some random value (in our logic perspective)

这将有助于避免大代码库中的随机问题,因为我们的代码可能会使用其他声明的具有一些随机值的变量(在我们的逻辑透视图中)

#3


1  

output file is generated by making object file of individually file and then linking them together by linker. Now when you have identical variable in two different file then individual file will compile with no error but at time of linking linker will get two definition of variable and generate an error. But In the case of static scope of both variable limited for the file so, every things works fine. I hope you will find this useful.

输出文件是通过制作单个文件的目标文件然后通过链接器将它们链接在一起而生成的。现在,当您在两个不同的文件中有相同的变量时,单个文件将编译而没有错误,但在链接时链接器将获得两个变量定义并生成错误。但是如果两个变量的静态范围限制了文件,那么每个东西都可以正常工作。我希望你会发现这很有用。

#4


0  

As far as I know, when you do not specify neither static nor extern then it's up to the compiler to choose. And gcc in this case goes for extern, thus you have to specify static in your case.

据我所知,当你既没有指定static也没有指定extern时,由编译器来决定。在这种情况下,gcc适用于extern,因此您必须在您的情况下指定static。

I had the same problem, a few years ago :-)

几年前我遇到了同样的问题:-)

#1


18  

So each .c file should have seen its own variable, right?

所以每个.c文件都应该看到自己的变量,对吧?

Wrong. In C, omitting static from a declaration means implicit extern linkage.

错误。在C中,省略声明中的静态意味着隐式的外部链接。

From C In a Nutshell:

来自C in a Nutshell:

The compiler treats function declarations without a storage class specifier as if they included the specifier extern. Similarly, any object identifiers that you declare outside all functions and without a storage class specifier have external linkage.

编译器在没有存储类说明符的情况下处理函数声明,就像它们包含说明符extern一样。类似地,您在所有函数外部声明但没有存储类说明符的任何对象标识符都具有外部链接。

#2


4  

ALWAYS initialize global variable, then compiler will not consider its linkage automatically as extern. Compiler will throw error during compilation.

总是初始化全局变量,然后编译器不会自动将其链接视为extern。编译器将在编译期间抛出错误。

This will help to avoid random problems in big code base ,because our code may use somebody else declared variable that has some random value (in our logic perspective)

这将有助于避免大代码库中的随机问题,因为我们的代码可能会使用其他声明的具有一些随机值的变量(在我们的逻辑透视图中)

#3


1  

output file is generated by making object file of individually file and then linking them together by linker. Now when you have identical variable in two different file then individual file will compile with no error but at time of linking linker will get two definition of variable and generate an error. But In the case of static scope of both variable limited for the file so, every things works fine. I hope you will find this useful.

输出文件是通过制作单个文件的目标文件然后通过链接器将它们链接在一起而生成的。现在,当您在两个不同的文件中有相同的变量时,单个文件将编译而没有错误,但在链接时链接器将获得两个变量定义并生成错误。但是如果两个变量的静态范围限制了文件,那么每个东西都可以正常工作。我希望你会发现这很有用。

#4


0  

As far as I know, when you do not specify neither static nor extern then it's up to the compiler to choose. And gcc in this case goes for extern, thus you have to specify static in your case.

据我所知,当你既没有指定static也没有指定extern时,由编译器来决定。在这种情况下,gcc适用于extern,因此您必须在您的情况下指定static。

I had the same problem, a few years ago :-)

几年前我遇到了同样的问题:-)