Re: Newbie Question - #include scope
Re: Newbie Question - #include scope
- Subject: Re: Newbie Question - #include scope
- From: Steve Checkoway <email@hidden>
- Date: Wed, 10 Dec 2008 12:22:23 -0800
On Wed, Dec 10, 2008 at 11:19:08AM -0800, Gil Dawson wrote:
> I made up a little project to demonstrate this thing. The project
> contains three files (listed below); "test.c" #includes the other two.
> The first #included file declares a global variable, MyData, and sets it.
> The second #included file uses that variable.
This isn't an Xcode question, it's a C question.
> The problem is, when I compile (command-K) the "test.c" file, the second
> #included file gets an error that MyData is undeclared. Why so?
My guess is that Xcode is trying to compile testadd.c as well as
including it into test.c. That's where you're getting the error.
> I confess to an illusion that this doesn't happen all the time. I could
> swear (but I cannot reproduce) that that compile went through without
> errors a few minutes ago, before I changed something then changed it
> back. Is this possible, or should I think about changing my coffee
> supplier?
I don't know about coffee, but you should pick up a book on C
programming.
To answer the question in your subject, #include copies the included
file verbatim into the including file (and then of course,
preprocesses it). You can see this for yourself by right clicking on
the file in Xcode and telling it to preprocess the file.
In general, you should not be including source files (.c, .cc, .cpp,
.m, .mm, etc.). You should only include header files (.h, etc.) (which
can themselves include other header files).
For your particular case, you should declare your global extern in the
header file, define it in exactly one of the source files, and both
source files should include the header (and not each other).
--
Steve Checkoway
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden