| | |
Working with header files ???
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 21
Reputation:
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: May 2005
Posts: 21
Reputation:
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
C Syntax (Toggle Plain Text)
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"
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: May 2005
Posts: 21
Reputation:
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
•
•
•
•
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..c Syntax (Toggle Plain Text)
extern int itemp;
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.
•
•
•
•
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: May 2005
Posts: 21
Reputation:
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
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...
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Including stdafx.h in other .cpp files (C++)
- Header files (C++)
- Header FIles (C++)
- c vs h header files... (C)
Other Threads in the C Forum
- Previous Thread: Program help please
- Next Thread: ---Importing text values in C -----
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






