• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie Question - #include scope
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Question - #include scope


  • Subject: Re: Newbie Question - #include scope
  • From: Jeremy Pereira <email@hidden>
  • Date: Wed, 10 Dec 2008 19:44:38 +0000


Xcode compiles test.c and testadd.c separately. You should not include testadd.c in test.c, and in fact, if your program had got as far as the linking stage, you would have had two instances of the function foo().


To make this work, you should

- delete your #include of testadd.c in test.c
- define MyData in one of the of the .c files
- change the line in test.h to
extern int MyData;
- include test.h in both .c files.
- find a good book on programming in C and review the differences between extern and static scoping.


On 10 Dec 2008, at 19:19, Gil Dawson wrote:

Hi--

I am rather new to XCode (Version 2.5), still reading the User Guide. I'm stumped by one thing I'm seeing that I'd think would be obvious.

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.

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?

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?

--Gil

//test.c
#include <test.h>
int main(void) {
	if (MyData); //stub
	return 1;
}
#include "testadd.c"

//test.h
int MyData = 0;

//testadd.c
void foo(void) {
	if (MyData);  //<-- 'MyData' undeclared (first use in this function)
}
_______________________________________________
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

_______________________________________________ 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
References: 
 >Newbie Question - #include scope (From: Gil Dawson <email@hidden>)

  • Prev by Date: Re: Which SDK is recommended when doing both Mac and iPhone development?
  • Next by Date: Re: Newbie Question - #include scope
  • Previous by thread: Re: Newbie Question - #include scope -- Thanks!
  • Next by thread: Re: Newbie Question - #include scope
  • Index(es):
    • Date
    • Thread