为什么包含防护不能防止链接器错误?
由于某种原因,即使我使用标头防护,我也会在标头文件中收到多个内容声明。我的示例代码如下:
main.c:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
thing.c:
#include "thing.h"
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
当我尝试编译此代码时,GCC 说我有 some 变量的多个定义。 ifndef 应该确保这种情况不会发生,所以我很困惑为什么会这样。
For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below:
main.c:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
thing.c:
#include "thing.h"
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
When I attempt to compile this, GCC says that I have multiple definitions of the something variable. ifndef should make sure that this doesn't happen, so I'm confused why it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
包含防护功能正常运行,并不是问题的根源。
发生的情况是,包含
thing.h
的每个编译单元都会获得自己的int Something = 0
,因此链接器会抱怨多个定义。以下是解决此问题的方法:
thing.c:
thing.h:
这样,只有
thing.c
才会有something
的实例,而main.c
将引用它。The include guards are functioning correctly and are not the source of the problem.
What happens is that every compilation unit that includes
thing.h
gets its ownint something = 0
, so the linker complains about multiple definitions.Here is how you fix this:
thing.c:
thing.h:
This way, only
thing.c
will have an instance ofsomething
, andmain.c
will refer to it.每个翻译单元中有一个定义(一个在
main.c
中,一个在thing.c
中)。标头防护阻止标头在单个翻译单元中多次包含。您需要在头文件中声明
something
,并且仅在thing.c
中定义它,就像功能:thing.c:
thing.h:
You have one definition in each translation unit (one in
main.c
, and one inthing.c
). The header guards stop the header from being included more than once in a single translation unit.You need to declare
something
in the header file, and only define it inthing.c
, just like the function:thing.c:
thing.h:
标头防护将阻止文件在同一编译单元(文件)中被多次编译。您将其包含在 main.c 和 thing.c 中,因此它将在每个单元中编译一次,从而导致变量
something
在每个单元中声明一次,或者总共声明两次。The header guards will stop the file from being compiled more than once in the same compilation unit (file). You are including it in main.c and thing.c, so it will be compiled once in each, leading to the variable
something
being declared once in each unit, or twice in total.尽量避免全局定义变量。
使用像increment()这样的函数来修改和读取它的值。
这样您就可以在 thing.c 文件中保持变量静态,并且您可以确定只有该文件中的函数才会修改该值。
try to avoid defining variables globally.
use functions like increment() to modify and read its value instead.
that way you can keep the variable static in the thing.c file, and you know for sure that only functions from that file will modify the value.
变量
something
应该在.c
文件中定义,而不是在头文件中。
仅变量和函数原型的结构、宏和类型声明
应该在头文件中。在您的示例中,您可以在头文件中将
something
的类型声明为extern int some
。但变量本身的定义应该在.c
文件中。根据您所做的操作,变量
something
将被定义在包含
thing.h
的每个.c
文件中,您会得到一个GCC 尝试链接时出现“多次定义的内容”错误消息
一切都在一起。
The variable
something
should be defined in a.c
file, notin a header file.
Only structures, macros and type declarations for variables and function prototypes
should be in header files. In your example, you can declare the type of
something
asextern int something
in the header file. But the definition of the variable itself should be in a.c
file.With what you have done, the variable
something
will be definedin each
.c
file that includesthing.h
and you get a"something defined multiple times" error message when GCC tries to link
everything together.
ifndef
所保护的是一个.h
多次包含在.c
中。比如的
事情。 h
thing2.h
main.c
如果我离开
ifndef
那么 GCC 会抱怨what
ifndef
is guarding is one.h
included in a.c
more than once. For instancething. h
thing2.h
main.c
if I leave
ifndef
out then GCC will complain