Re: external declares
Re: external declares
- Subject: Re: external declares
- From: Charlton Wilbur <email@hidden>
- Date: Sat, 11 Feb 2006 18:18:27 -0500
On Feb 11, 2006, at 5:23 PM, Wes Peterson wrote:
1) Exactly what is XCode, (and what isn't)? I installed the
developer tools on my Mac and I got a lot of things, including
the development environment, gcc and the linker, and also
a lot of libraries, including at least Cocoa, which I am
interested in. My first idea was that all of this is XCode,
but that seems not to be the case and it is not clear.
Here's my answer: XCode is the application that is the development
environment and front end to the build tools. The editor, the
project-management features, the data modeller, that's all XCode. It
fits the same place in the developer's toolchain as Emacs and
Makefiles fit in earlier Unix development.
gcc is not XCode, because it is used extensively apart from XCode.
The C language is not XCode, because it is used even more extensively
apart from XCode. Cocoa is not XCode, because it can be used from
Codewarrior or from Python, Java, or Perl.
If your question and problem would be the same whether you're using
XCode, Codewarrior, Eclipse, or Emacs with Makefiles, it's not an
issue with XCode. There's a lot of grey area, but questions about C
syntax and semantics most likely aren't specific to XCode.
2) This may not be XCode, but I would appreciate any insight
or good pointers to where to get it. I have been using C since
before the 1988 standard was adopted. Then the rules were very
clear. Every variable and function must be defined exactly once
in a program and declared (or defined) in every module in which
it is used. So for external variables, you had to have an extern
statement in each module before the variable is used and a
definition exactly once in the whole program. Recently I
downloaded the 1999 standard and read it carefully, and as
closely as I could tell, nothing has changed. It used to be that
if you put a definition for an external variable in an include
file and include it in more than one module, you get a multiply-
defined variable message. Recently, I see programs in which
external variables are defined in include files and included
multiple times and the program works fine. Sometimes when I
try that it works and sometimes it doesn't. I don't understand
the rules.
It's not XCode, but here's an answer.
That aspect of the standard has not changed, but compilers are often
lenient in what they accept. Follow the rules, and you'll be writing
standard C that works everywhere. Break them, and you're dependent
on the quirks of the particular compiler you're using.
If I had to explain the behavior based on the standard, I'd refer to
the C99 spec, section 6.2.2, and find out the following: a variable
defined with 'extern' and a variable defined with no storage-class
specifier ('extern' or 'static', mainly, for the purposes of this
argument) both have external linkage. Thus each instance of an
object with the same name and external linkage refers to the same
object. Section 6.9, point 5 uses the distinction between
*declaration* and *definition* -- a definition is a declaration that
also causes storage space to be reserved -- and declares - "If an
identifier declared with external linkage is used in an
expression ..., somewhere in the entire program there shall be
exactly one external definition for the identifier; otherwise, there
shall be no more than one." Section 6.9, point 2 defines a
"tentative definition," which is a definition with neither storage-
class specifier nor initializer, and which is assumed to have
external linkage -- if you reach the end of a translation unit with a
tentative definition of an identifier but without an external
declaration, then you get an external definition automatically with
an initializer of 0.
In other words, according to the standard, you can *declare* things
as many times as you like; once you do something that allocates
space, they're *defined*, and you can only do that once per program.
And declaring something at file scope without an 'extern' or 'static'
qualifier and without an initializer means it has space allocated for
it and a default initializer of 0. You can say "extern int foo;" or
even "int foo;" as many times as you like in one file; the moment you
say "int foo = 7;" more than once or have more than one file in which
you say "int foo;" you have a problem.
The best places to get plainer English clarification on this are the
same as they have always been: Kernighan and Ritchie's _The C
Programming Language_ and Harbison & Steele's _C: A Reference
Manual_. Very little has changed since C89 to invalidate either one
of those.
Charlton
--
Charlton Wilbur
email@hidden
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