I saw some similar questions, but in none of them, the #ifndef HEADER_H
was mentioned.
I have a header file and 2 C files:
constants.h
main.c
mylib.c
In constants.h:
#ifndef CONSTANTS_H
#define CONSTANTS_H
const int NUM_OF_ITEMS = 22;
#endif
In mylib.c:
#include "constants.h"
... code ...
In main.c:
#include "constants.h"
int main() {
... code ...
}
When I compile using the command: gcc main.c mylib.c -o main
, I get the following error:
/tmp/ccl55fv3.o:(.rodata+0x0): multiple definition of `NUM_OF_ITEMS'
/tmp/ccyZhu6F.o:(.rodata+0x0): first defined here
collect2: ld returned 1 exit status
#ifndef
, so why it happens??constants.h
for declaration and constants.c
for assignment?ANSWER. Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.
static is a guarantee that a variable gets internal linkage. Thus other translation will not be able to access it or declare extern variables referring to it. What happens if you declare a static variable in a header file is that more than one translation unit will get a separate variable with that name.
Use of the Global Variable in C The global variables get defined outside any function- usually at the very top of a program. After this, the variables hold their actual values throughout the lifetime of that program, and one can access them inside any function that gets defined for that program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.
Each translation unit (read .c source file) into which you include the header file results in a new definition of the global variable. That is why the linker objects to there being multiple definitions. Remember that #include
performs text insertion of the included file. From the perspective of the compiler, each of your translation units contains distinct definitions of NUM_OF_ITEMS
.
You need to define it in exactly one translation unit. You might consider adding a constants.c
which contained the definition of NUM_OF_ITEMS
, and leaving just a declaration in the header file.
Alternatively you could use a macro:
#define NUM_OF_ITEMS 22
Or as Jens suggests in the comment below, an enumeration constant.
Or as you yourself suggest, with static
linkage so that the object is internal to each translation unit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With