Please support our C advertiser: Programming Forums
Views: 2359 | Replies: 11
![]() |
•
•
Join Date: May 2005
Posts: 21
Reputation:
Rep Power: 4
Solved Threads: 0
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
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
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
•
•
Join Date: May 2005
Posts: 21
Reputation:
Rep Power: 4
Solved Threads: 0
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.
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
This 'll probably resolve the issue..
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..
Michelangelo
"The best place to find a helping hand is at the end of your own arm"
"The best place to find a helping hand is at the end of your own arm"
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,565
Reputation:
Rep Power: 40
Solved Threads: 977
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:
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.
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.
•
•
Join Date: May 2005
Posts: 21
Reputation:
Rep Power: 4
Solved Threads: 0
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
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
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
•
•
•
•
Hi,
I would like to add here, that to use the variable in the file link.c , try to declare it again as follows
c Syntax (Toggle Plain Text)
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 ??
Last edited by thekashyap : Mar 28th, 2007 at 8:27 am.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,565
Reputation:
Rep Power: 40
Solved Threads: 977
•
•
•
•
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.
•
•
Join Date: May 2005
Posts: 21
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode