hi,
can u tell me y linker gives error when we try to define a variable in a header file and include this in more than one source file?

/*test.h*/
int i =0;

/*1.c*/
#include "test.h"

i++;

/*2.c*/
#include "test.h"

i++;

whereas the same works fine if i use static in header file.

i can understand that this gives no favour. still while discussing with my colleagues i came across this. can any one answer this?

Recommended Answers

All 3 Replies

>can u tell me y linker gives error when we try to define a variable
>in a header file and include this in more than one source file?
C allows you to declare a variable any number of times, but you can only define it once. If you place a definition in a header and include that header multiple times, you've defined something more than once, which is an error.

>whereas the same works fine if i use static in header file.
The preprocessor is nothing more than a fancy cut and paste. If you make the definition static in your header, then it'll be static in the file that includes the header. static definitions are local to the file, so you're not really defining the variable multiple times in this case. However, each file that includes the header gets a separate instance of the variable, so you can't expect changes to i in 1.c to affect i in 2.c.

Hi Narue,

Thanks for the reply. but still i have 1 doubt.

>can u tell me y linker gives error when we try to define a variable
>in a header file and include this in more than one source file?
C allows you to declare a variable any number of times, but you can only define it once. If you place a definition in a header and include that header multiple times, you've defined something more than once, which is an error.

if i declare my variable and define in more than 1 file, it works. Thats because, as we included our header file into the source it makes local copies(I trust I am correct here). so

/*test.h*/
int i;

/*1.c*/
#include "test.h"

i = 5;

/*2.c*/
#include "test.h"

i = 10;

works well. can u clarify this???
if this is working, u can't we define variable in header (as it should also make a local copy of the variable in each file it is declared)

>whereas the same works fine if i use static in header file.
The preprocessor is nothing more than a fancy cut and paste. If you make the definition static in your header, then it'll be static in the file that includes the header. static definitions are local to the file, so you're not really defining the variable multiple times in this case. However, each file that includes the header gets a separate instance of the variable, so you can't expect changes to i in 1.c to affect i in 2.c.

> works well. can u clarify this???
They're called "tentative definitions".
http://david.tribble.com/text/cdiffs.htm#C99-odr

In C, you can do int i; as many times as you like, in as many files as you like, and C will turn ONE of them into int i = 0; and all the rest into extern int i; But as soon as you have an explicit definition in a header file (say with an assignment), then you're back to where you started.

Note the difference, C++ does NOT allow this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.