问题
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?
回答1:
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:
// 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:
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.
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).
Anyway, the question has been asked many times before. See constant variables not working in header or do a search.
回答3:
Every c-file which includes your header file has the line
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
extern const struct dsm_config DEFAULT_DSM_CONFIG;
and add the definition
const struct dsm_config DEFAULT_DSM_CONFIG = { AUTO_UNMAP, NO_ENABLE_COA };
in only one c file.
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 };
Then each c-file defines it's own dsm_config which is not visible to other translation units during link time.
回答4:
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.
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.
来源:https://stackoverflow.com/questions/14526153/multiple-definition-of-a-global-variable