Hi All

I have a header file link.h , in which i declare a variable itemp.

When i assign this variable a value in the C file link.C i get an error during compilation:

cxx: Error: link.C, line 5: this declaration has no storage class or type
specifier
itemp=0;
^
cxx: Error: link.C, line 5: declaration has already been defined by variable
"itemp" (declared at line 3 of "/home/team/.shared/link.h")
itemp=0;


Plz help


Thnx

Seems like you've declared the variable like this:
itemp=0;

If that's the case it's wrong and it should be something like:
int itemp ;

If that's not the case please post the exact declaration of this variable from link.h

Hi

Thnx 4 the reply ....

Actually I was not initializing this variable within a function in my C file. Does That means that the variables declared in a header file are visible in a function only ??

Else they are considered global for that C file if defined outside any function...and need to be declared before definition.

Hi,
I would like to add here, that to use the variable in the file link.c , try to declare it again as follows

extern int itemp;

This 'll probably resolve the issue..

you should never actually declare variables in header files because if you have two or more *.c files that include that header file the compiler linker will issue duplicate objects errors (or something like that).

To get around that problem, in header files use the extern keyword, as in bala's example: extern int itemp; Then in ONE and only ONE *.c or *.cpp file declare it again but without the extern keyword. This will actually create an instance of the variable.

Hi

I tried making two static libraries each containing a single object file (abc1.C and abc2.C).
and libs: lib1.a (with abc1.o) and lib2.a (with abc2.o). Both the C files include the same header file and access the same variable "global".

I access this variable from both the files and find that a single copy of the variable is used , since a increment to variable "global" in one is seen in other file also.

I have declared the variable global as
int global;

Now everything is working perfectly.

Still do i need to declare this variable as an extern ??? Why am i not getting the multiple declaration error ???

Thnx

Hi,
I would like to add here, that to use the variable in the file link.c , try to declare it again as follows

extern int itemp;

This 'll probably resolve the issue..

and why would you do this???
If variable is declared in link.h (which of course isn't a good idea) and link.h is #included in link.C there is no logic in doing this.

If link.h is not included in link.C (which I very much doubt looking at their names) then one should do what you suggest.
But then again see the errors he reported. To me at least it seems clear that link.h is included in link.C:
cxx: Error: link.C, line 5: this declaration has no storage class or type
specifier
itemp=0;
^
cxx: Error: link.C, line 5: declaration has already been defined by variable
"itemp" (declared at line 3 of "/home/team/.shared/link.h")
itemp=0;

@@gpta_varun
I don't think your code have any issues with scope etc. It's seems like a simple "wrong declaration".
Did you actually try changing declaration in link.h to "int itemp;" ??
Can you post the declaration from link.h ??

Hi

I tried making two static libraries each containing a single object file (abc1.C and abc2.C).
and libs: lib1.a (with abc1.o) and lib2.a (with abc2.o). Both the C files include the same header file and access the same variable "global".

I access this variable from both the files and find that a single copy of the variable is used , since a increment to variable "global" in one is seen in other file also.

I have declared the variable global as
int global;

Now everything is working perfectly.

Still do i need to declare this variable as an extern ??? Why am i not getting the multiple declaration error ???

Thnx

If you declare variables with the same variable names in two or more program units and the variables are of the same type, some compiler will combine them as if one was declared with the extern keyword. If you don't want that behavior then use the static keyword and each program unit will have its own copy of the variables.

If you declare variables with the same variable names in two or more program units and the variables are of the same type, some compiler will combine them as if one was declared with the extern keyword. If you don't want that behavior then use the static keyword and each program unit will have its own copy of the variables.

Declaring this variable as an extern give an error

ld:
Unresolved:
global

while compiling the 2 C files .

thnx

Either you forgot to put the actual variable declaration (the one without the extern keyword) in one of the source files, or you forgot to feed that file to the linker...

A word on unresolved symbol:
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you

May the concept of symbols and linkage and scope isn't clear to you.
- Say you have a.h/a.c b.h/b.c and main.c
- Say main.c #includes a.h and b.h and uses the functions declared there.
- You need to have a variable "int global_var ;" that you need to use in all 3 places (main, a, b).

So the question is where should you put int global_var ; ?
- You are going to compile 3 files a/b/main.c and create a/b/main.o. These 3 are your translation units (or program units as Ancient Dragon refer to it)
- You should define (for a non-static basic type independent variable declaration is also definition) only ONE int global_var; because you want all 3 of them to use the same variable not a different one.
- So what you do is, define it in ONE place and put some special statement in other two places that promises compiler that this variable will be defined at link time, so ignore for the time being.
- This special statement consists of keyword extern. When you prefix some declaration with this word it tells compiler that it is NOT defined in this translation unit, but it'll be defined at link time in one of the other translation unit with which I'll link this translation unit.

In short now to solve problem I described,
* put "int global_var;" in main.c.
* put "extern int global_var ;" in a.c
* put "extern int global_var ;" in b.c

A word on "multiple definitions of a symbol": When you miss out on the word extern, all your .c files will compile without errors, but now you will have 3 compiled units main/a/b.o each containing definition of a symbol global_var. So when linker is trying link them together it sees that same symbol (int global_var) is defined multiple times, which isn't allowed and so it cribs..
Whether you "directly put int global_var in a/b.c" or by "putting int global_var in a/b.h and then #including a/b.h in a/b.c" does not matter end result is the same (multiple definitions of same symbol at link time)

A word on "unresolved symbol":
This is the oppisite of "multiple definitions". In this case you have put extern declaration in some file, (so compiler is happy and leaves the work of finding it's definition to liner). But because none of the translation units you gave to linker for linking has defined it.
"extern int global_var" is declaration/promise.
"int global_var" is definition.

commented: A little rep for ya --joeprogrammer +8

Thnx thekashyap for such a comprehensive explanation.

It was quite useful.

Thnx Again

-varun

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.