944,088 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5867
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 28th, 2007
0

Working with header files ???

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 28th, 2007
0

Re: Working with header files ???

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
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 28th, 2007
0

Re: Working with header files ???

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 28th, 2007
0

Re: Working with header files ???

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..
Reputation Points: 15
Solved Threads: 11
Junior Poster
bala24 is offline Offline
125 posts
since Oct 2006
Mar 28th, 2007
0

Re: Working with header files ???

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 28th, 2007
0

Re: Working with header files ???

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 28th, 2007
0

Re: Working with header files ???

Click to Expand / Collapse  Quote originally posted by bala24 ...
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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 28th, 2007
0

Re: Working with header files ???

Click to Expand / Collapse  Quote originally posted by gpta_varun ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 29th, 2007
0

Re: Working with header files ???

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 29th, 2007
0

Re: Working with header files ???

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...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Program help please
Next Thread in C Forum Timeline: ---Importing text values in C -----





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC