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?
有什么想法吗?
4 个解决方案
#1
29
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.
这将解决您的问题。
#2
5
In C language const
objects 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_CONFIG
with 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 internal linkage) or remove the definition from the header file (leave only a non-defining extern const
declaration there).
将对象声明为静态const(如果你不介意有多个具有内部链接的对象)或从头文件中删除定义(在那里只留下一个非定义的extern const声明)。
Anyway, the question has been asked many times before. See constant variables not working in header or do a search.
无论如何,这个问题之前已被问过很多次了。查看不在标头中工作的常量变量或进行搜索。
#3
3
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;
extern const struct 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 };
static const struct 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,在链接时间内其他翻译单元不可见。
#4
2
Each source file (.c, not .h) is compiled separately. In each of these compilations, the declaration of dsm_config
with an initializer (the = { values… }
portion) creates a definition of dsm_config
. Thus, the whole program has multiple definitions.
每个源文件(.c,而不是.h)都是单独编译的。在每个这些编译中,使用初始化程序(= {values ...}部分)声明dsm_config会创建dsm_config的定义。因此,整个程序有多个定义。
Generally, header files should only declare objects and not define them. To do this, remove the initializer in the header file, leaving just the declaration with no initializer. In one source file, define dsm_config
by repeating the declaration with the initializer.
通常,头文件应该只声明对象而不是定义它们。为此,请删除头文件中的初始化程序,只留下没有初始化程序的声明。在一个源文件中,通过使用初始化程序重复声明来定义dsm_config。
#1
29
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.
这将解决您的问题。
#2
5
In C language const
objects 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_CONFIG
with 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 internal linkage) or remove the definition from the header file (leave only a non-defining extern const
declaration there).
将对象声明为静态const(如果你不介意有多个具有内部链接的对象)或从头文件中删除定义(在那里只留下一个非定义的extern const声明)。
Anyway, the question has been asked many times before. See constant variables not working in header or do a search.
无论如何,这个问题之前已被问过很多次了。查看不在标头中工作的常量变量或进行搜索。
#3
3
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;
extern const struct 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 };
static const struct 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,在链接时间内其他翻译单元不可见。
#4
2
Each source file (.c, not .h) is compiled separately. In each of these compilations, the declaration of dsm_config
with an initializer (the = { values… }
portion) creates a definition of dsm_config
. Thus, the whole program has multiple definitions.
每个源文件(.c,而不是.h)都是单独编译的。在每个这些编译中,使用初始化程序(= {values ...}部分)声明dsm_config会创建dsm_config的定义。因此,整个程序有多个定义。
Generally, header files should only declare objects and not define them. To do this, remove the initializer in the header file, leaving just the declaration with no initializer. In one source file, define dsm_config
by repeating the declaration with the initializer.
通常,头文件应该只声明对象而不是定义它们。为此,请删除头文件中的初始化程序,只留下没有初始化程序的声明。在一个源文件中,通过使用初始化程序重复声明来定义dsm_config。