Jump to content
  • Advertisement

Archived

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

3flares

c newbie, compiling problem

This topic is 5787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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:

No, no, no, none of that "extern" or "separate header file" stuff is necessary.



Wow. I''m speechless.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!