c newbie, compiling problem

Started by
12 comments, last by 3flares 21 years, 8 months ago
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 ?
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.
this is how you use global variables:


    // someheader.h// declare variables with extern keywordextern 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]
My compiler generates one error message: "does not compile."
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.hint 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]
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
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]
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
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
[Cyberdrek | ]
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".
quote:
No, no, no, none of that "extern" or "separate header file" stuff is necessary.


Wow. I''m speechless.
daerid@gmail.com

This topic is closed to new replies.

Advertisement