 |
 |
C, Globals, and Project Builder
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Status:
Offline
|
|
I'm having some problems writing this C program in project builder. It is not a Cocoa or Carbon app, but just a C tool. The problem lies in my global declarations. I had them declared in a header file that was included in my one source file and this worked fine. However, when I add another source file, and include that header file, project builder then fails to build the program complaining about multiple definitions of the globals. I can compile it at the command line using gcc fine, but I would rather stay within project builder. Am I going about this the wrong way? Any help here would be much appreciated.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status:
Offline
|
|
If I read your post correctly, you have something like
int foo;
in your header file. The problem is, when the header is included in 2 source files the compiler is generating 2 definitions of the variable foo. For that reason, it's a bad idea to define variables in a header file. Instead, declare the variable in the header file like
extern int foo;
This tells the compiler that a variable foo exists (but this isn't the definition). Then in one of your source files, define foo
int foo;
and everything should work nicely.
Matt
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Status:
Offline
|
|
I think you got the idea of my post, except that I do not define the variables in the header file, only declare them. Is the extern keyword specific to objective c? I'm just writing straight C. I'm not sure I understand. Do I have to then redeclare each variable in each source file? If I put:
extern int foo;
in my header, and then in foo.c:
int foo;
can I then access foo in bar.c?
-Zach
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jan 2000
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2001
Status:
Offline
|
|
Originally posted by zpaine:
<STRONG>I think you got the idea of my post, except that I do not define the variables in the header file, only declare them. Is the extern keyword specific to objective c? I'm just writing straight C. I'm not sure I understand. Do I have to then redeclare each variable in each source file? If I put:
extern int foo;
in my header, and then in foo.c:
int foo;
can I then access foo in bar.c?
-Zach</STRONG>
"extern" is a C keyword used for giving variables a global scope outside of just the file which the variable is declared.
Normal global variables are only accessible from inside the file they are declared in. Using extern removes this restriction, it's often used in OpenGL programming because much of graphics programming is done using global variables.
The described fix to your issue ought to work fine.
Hope that helps,
Nathan
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Boulder, CO, USA
Status:
Offline
|
|
Originally posted by NoAuthoritaw:
<STRONG>
"extern" is a C keyword used for giving variables a global scope outside of just the file which the variable is declared.
Normal global variables are only accessible from inside the file they are declared in. Using extern removes this restriction, it's often used in OpenGL programming because much of graphics programming is done using global variables.
</STRONG>
Well... perhaps this is nit-picky, but I think it's more accurate to say that <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>int foo;</font>[/code] written in global scope does in fact make foo globally accesible by default. Anyone can use the symbol "foo" to refer to the space set aside by the compiler for that int.
However, since each source file is compiled independently of others, you can't go around using the name "foo" out of context, because the compiler won't know foo's type. This is where extern comes in. You may use extern to tell the compiler what foo's type is as it compiles other sources so that it will know how to treat that name when you use it. The compiler can only assume you know what you're doing and that foo has really been defined somewhere.
If foo is in fact defined somwhere, the linker will resolve the symbol where necessary at link-time. If it's not, or if you've defined two variables named foo in the same scope, the linker will complain ("undefined symbol" or "multiple definitions of symbol", respectively). This is the error zpaine was first seeing. He had two globally-scoped variables named foo.
So extern really just pacifies the compiler: "There's this thing called foo of type int defined somewhere else and I want to use it; you don't need to know any more than that."  It doesn't change the scope of the variable to which you're referring.
As an aside, <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>static int foo;</font>[/code] would prevent the variable from being accesible outside of the source file in which it's declared.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2002
Status:
Offline
|
|
I'd like to thank everyone for their replies. My program is now working wonderfully using extern. Thanks again!!
-Zach
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|