C语言 一个全局变量的多重定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14526153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple definition of a global variable
提问by Steve Walsh
Possible Duplicate:
constant variables not working in header
可能的重复:
常量变量在标题中不起作用
In my header file which I use to create a shared object, I have the following:
在我用来创建共享对象的头文件中,我有以下内容:
#ifndef LIB_HECA_DEF_H_
#define LIB_HECA_DEF_H_
struct dsm_config {
int auto_unmap;
int enable_copy_on_access;
};
enum { NO_AUTO_UNMAP, AUTO_UNMAP } unmap_flag;
enum { NO_ENABLE_COA, ENABLE_COA } coa_flag;
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
<more code ...>
#endif
When I compile, I get the following error:
当我编译时,我收到以下错误:
cc -g -Wall -pthread libheca.c dsm_init.c -DDEBUG master.c -o master
/tmp/cciBnGer.o:(.rodata+0x0): multiple definition of `DEFAULT_DSM_CONFIG'
/tmp/cckveWVO.o:(.rodata+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [master] Error 1
Any ideas why?
任何想法为什么?
回答by junix
Because with every include in a implementation file file, a new instance of your struct is created (and stored in the object file).
因为对于实现文件文件中的每个包含,都会创建一个新的结构实例(并存储在目标文件中)。
To avoid this, just declare the struct as "extern" in the header file and initialize it in the implementation file:
为避免这种情况,只需在头文件中将结构声明为“extern”并在实现文件中对其进行初始化:
// In your header file:
extern const struct dsm_config DEFAULT_DSM_CONFIG;
// In your *.c file:
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
This will solve your problem.
这将解决您的问题。
回答by AnT
In C language constobjects have external linkage by default (as opposed to C++ where they have internal linkage by default). So, in your code you created multiple definitions of an object DEFAULT_DSM_CONFIGwith external linkage - a clear violation of definition rules of C language.
在 C 语言中,const对象默认具有外部链接(与默认情况下具有内部链接的 C++ 相反)。因此,在您的代码中,您创建了多个DEFAULT_DSM_CONFIG具有外部链接的对象定义——这明显违反了 C 语言的定义规则。
Either declare your object as static const(if you don't mind having multiple objects with internallinkage) or remove the definition from the header file (leave only a non-defining extern constdeclarationthere).
要么将您的对象声明为static const(如果您不介意有多个具有内部链接的对象),要么从头文件中删除该定义(只留下一个非定义extern const声明)。
Anyway, the question has been asked many times before. See constant variables not working in headeror do a search.
无论如何,这个问题之前已经被问过很多次了。请参阅在标题中不起作用的常量变量或进行搜索。
回答by Werner Henze
Every c-file which includes your header file has the line
每个包含头文件的 c 文件都有一行
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
So each of these c files defines a variable dsm_config. If you want only one variable dsm_config you need to change the declaration in the header file to
所以这些 c 文件中的每一个都定义了一个变量 dsm_config。如果您只需要一个变量 dsm_config,则需要将头文件中的声明更改为
extern const struct dsm_config DEFAULT_DSM_CONFIG;
外部 const 结构 dsm_config DEFAULT_DSM_CONFIG;
and add the definition
并添加定义
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
in only one c file.
只有一个c文件。
Another, not so good solution is to make change the header file to
另一个不太好的解决方案是将头文件更改为
static const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
静态常量结构 dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
Then each c-file defines it's own dsm_config which is not visible to other translation units during link time.
然后每个 c 文件定义它自己的 dsm_config,它在链接期间对其他翻译单元不可见。
回答by Eric Postpischil
Each source file (.c, not .h) is compiled separately. In each of these compilations, the declaration of dsm_configwith an initializer (the = { values… }portion) creates a definition of dsm_config. Thus, the whole program has multiple definitions.
每个源文件(.c,不是 .h)都是单独编译的。在这些编译中的每一个中,dsm_config带有初始值设定项(= { values… }部分)的声明创建了dsm_config. 因此,整个程序有多个定义。
Generally, header files should only declareobjects and not definethem. To do this, remove the initializer in the header file, leaving just the declaration with no initializer. In one source file, define dsm_configby repeating the declaration with the initializer.
一般来说,头文件应该只声明对象而不是定义它们。为此,请删除头文件中的初始化程序,只留下没有初始化程序的声明。在一个源文件中,dsm_config通过使用初始化程序重复声明来定义。

