Working with header files ???

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Working with header files ???

 
0
  #1
Mar 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Working with header files ???

 
0
  #2
Mar 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

 
0
  #3
Mar 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: Working with header files ???

 
0
  #4
Mar 28th, 2007
Hi,
I would like to add here, that to use the variable in the file link.c , try to declare it again as follows

  1. 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"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Working with header files ???

 
0
  #5
Mar 28th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

 
0
  #6
Mar 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Working with header files ???

 
0
  #7
Mar 28th, 2007
Originally Posted by bala24 View Post
Hi,
I would like to add here, that to use the variable in the file link.c , try to declare it again as follows

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Working with header files ???

 
0
  #8
Mar 28th, 2007
Originally Posted by gpta_varun View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

 
0
  #9
Mar 29th, 2007
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Working with header files ???

 
0
  #10
Mar 29th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC