Archived

This topic is now archived and is closed to further replies.

3flares

c newbie, compiling problem

Recommended Posts

i have a problem with global variables in gcc i put 2 global variables in a header file, but when i try to use this in other source files i get an error that says Undefined reference to (some var) and can''t get the program compile. anybody know y does this happen ?

Share this post


Link to post
Share on other sites
If you have global variables that you want any source file to be able to access, you need to declare them in their own header file, like globals.h.


// globals.h
//

// prevent multiple inclusion
#ifndef _globals_h_
#define _globals_h_

int g_count;
long g_someData;
char *g_str;
...

#endif


Then just #include <globals.h> There may be some other issues that I''m not sure of (I don''t normally do this, I stick with classes), but it should get you in the right direction.

Share this post


Link to post
Share on other sites
this is how you use global variables:


    
// someheader.h

// declare variables with extern keyword

extern char* playername;
extern int playerstate;

// source1.cpp

// redeclare variables without extern keyword

// and use them

#include "someheader.h"
char* playername=0;
int playerstate=0;

plyaerstate = 12;
if (playername) strcpy(playername, "Jim");

// source2.cpp

// do not need to redeclare them

#include "someheader.h"
playerstate = 10;
if (playername) strcpy(playername, "Matt");



extern keyword tells compiler that such variable exists somewhere else, thus making it global.


My compiler generates one error message: "does not compile."

EDIT:
forgot to put #include <img src="smile.gif" width=15 height=15 align=middle>

[edited by - nicho_tedja on August 14, 2002 3:38:41 PM]

Share this post


Link to post
Share on other sites
No, no, no, none of that "extern" or "separate header file" stuff is necessary. All you need to do is declare the variables in the header file -- doesn't matter whether they're by themselves or with other stuff -- and then work with it in your code, f'rinstance:
The header:

      
//header.h

int variable1;
char variable2[10];

and the .c file:

//myfile.c

#include "header.h"
int main(int argc, char *argv[])
{
variable1 = 1373;
sprintf(variable2, "Hello world.");
printf("Variable 1: %d; Variable 2: %s", variable1, variable2);
return 0;
}

None of that may be strictly ANSII programming, but it should work completely well. If you have other code files, you can include header.h in them also, and have complete access to the variables there.

EDIT: If you're already doing this, then maybe you should post your source code and let us examine it.

Twilight Dragon
Win32 API Programmer
www.freewebz.com/j-world

[edited by - TDragon on August 14, 2002 5:32:08 PM]

[edited by - TDragon on August 14, 2002 5:46:10 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by TDragon
No, no, no, none of that "extern" or "separate header file" stuff is necessary.


Ummm... have you ever compiled a program with more than one object file? Please don''t spread misinformation like this. extern is there for a reason, and this is that reason.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
Yes, I've compiled the game I'm currently working on, containing 5 object files, all accessing the same variables from the main header file, many times. For your information, it is currently working quite nicely (other than that I can't load sprites because I'm trying to implement a new file format). Do you still think that the "extern" is necessary?

EDIT: Oh, and do you think I could have gotten along all these years using this same approach to make complex Windows applications, and not have noticed something wrong? Okay, maybe I'm overreacting a little. It probably is a good idea to use the extern keyword (it'll give the compiler one less location headache, for what it's worth), but I'm just trying to say that it isn't NECESSARY. In case you think my compiler is messed, know that I also use GCC.

Twilight Dragon
Win32 API Programmer
www.freewebz.com/j-world

[edited by - TDragon on August 14, 2002 5:50:35 PM]

Share this post


Link to post
Share on other sites
Ahh, interesting: gcc allows redefinition of global variables. I wasn''t aware of this; I''ve been spending most of my time developing programs that need to compile across various platforms (in particular, SGI''s braindead C-compiler), and some of these architectures require the explicit extern-declaration.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by TDragon
Yes, I''ve compiled the game I''m currently working on, containing 5 object files, all accessing the same variables from the main header file, many times. For your information, it is currently working quite nicely (other than that I can''t load sprites because I''m trying to implement a new file format). Do you still think that the "extern" is necessary?

EDIT: Oh, and do you think I could have gotten along all these years using this same approach to make complex Windows applications, and not have noticed something wrong? Okay, maybe I''m overreacting a little. It probably is a good idea to use the extern keyword (it''ll give the compiler one less location headache, for what it''s worth), but I''m just trying to say that it isn''t NECESSARY. In case you think my compiler is messed, know that I also use GCC.

Twilight Dragon
Win32 API Programmer
www.freewebz.com/j-world

[edited by - TDragon on August 14, 2002 5:50:35 PM]


You know what, you''re wrong. In order to be able to access those variables in different source files, you need the extern keyword in your header file. It''s been that way even back in the old DOS days. If you still think you don''t need the extern keyword, take a good look at any good programming book.





"DaHjajmajQa''jajHeghmeH!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

Share this post


Link to post
Share on other sites
quote:
Original post by TDragon
No, no, no, none of that "extern" or "separate header file" stuff is necessary. All you need to do is declare the variables in the header file

Yes, but without extern you have both declaration and definition, giving rise to multiple definition link-time errors when you include the header in more than one translation unit. Use extern to say "this is a declaration only".

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
Ahh, interesting: gcc allows redefinition of global variables. I wasn''t aware of this; I''ve been spending most of my time developing programs that need to compile across various platforms (in particular, SGI''s braindead C-compiler), and some of these architectures require the explicit extern-declaration.



Don''t take his words like that... the man was wrong and missunderstanding something. mabe he is declaring these variables in a header but for the link.exe to work properly this header must have been included only in one of the source files!

still use the extern keyword. its the right way as was mentioned before.

Share this post


Link to post
Share on other sites
Aarg, I''m constantly mis-quoted! What I am trying to show is that a specific compiler (gcc) doesn''t give any errors or any kind of warning whatsoever when globals from separate files are used without an extern keyword. I''m NOT saying that it isn''t a good idea to use extern (you must if you''re following ANSI programming standards), NOR am I recommending that everyone drop using it. Once again, all I''m trying to say is that 3flares'' original error is NOT due to lack of the extern keyword. Sheesh.

And to those of you who seem to assume that not extern-ing those globals just won''t work (Sabreman and Cyberdrek): why the heck to you think I''ve gotten away with it for so long using one of the most popular, continually-updated compilers, and not noticed anything wrong?

Twilight Dragon
Win32 API Programmer
www.freewebz.com/j-world

Share this post


Link to post
Share on other sites
The goal is to remain as compliant to standards as possible, whether or not you have to. While it may work in GCC, it won't very well with other compilers/linkers.

I'm not saying you don't know what your doing (hosestly, I haven't used GCC before so I have to take your word that it works), but just that it's a good idea to remain as portable as you can.

[edited by - Zipster on August 16, 2002 6:36:29 PM]

Share this post


Link to post
Share on other sites