Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I declare and assign global variable in a header file, even when using #ifndef HEADER_H

Tags:

c

gcc

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
  • I mentioned #ifndef, so why it happens??
  • Is there anything to do except for splitting it to constants.h for declaration and constants.c for assignment?
like image 653
SomethingSomething Avatar asked Oct 20 '14 08:10

SomethingSomething


People also ask

Can you declare global variables in a header file?

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.

What happens if we include a static global variable in a header file?

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.

Can I declare a global variable inside a function in C?

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.

Can we declare global variable after main function?

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.


1 Answers

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.

like image 109
David Heffernan Avatar answered Oct 16 '22 03:10

David Heffernan