RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 2358 | Replies: 11
Reply
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Working with header files ???

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Working with header files ???

  #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  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

  #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  
Join Date: Oct 2006
Location: India
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: Working with header files ???

  #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

 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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,565
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Working with header files ???

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

  #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  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Working with header files ???

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,565
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Working with header files ???

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: Working with header files ???

  #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  
Join Date: Apr 2006
Location: Canada
Posts: 4,545
Reputation: John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all John A is a name known to all 
Rep Power: 17
Solved Threads: 284
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Working with header files ???

  #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...
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:04 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC