Skip to main content
GameDev.net gamedev.net
🔒 Locked

global reference

Started by datalurkur Dec 13, 2005 at 6:28 PM 9 replies 1.6k views
Original Post
datalurkur
datalurkur
I need to create variables for height and width that can be referenced by any file in my project, but am having problems using an include file because of linker errors. Anyone have any advice about how to create global variables or avoid getting linkers another way?
SiCrane
SiCrane
When declaring a global variable in a header file you need to use the extern keyword. And then in one source file you need to define the variable at namespace scope (not inside a class definition or function).
Ex:
header.h
extern int my_int;

my_file.cpp
int my_int = 0;


For more details see this article.
jflanglois
jflanglois
Quote:
For more details see this article.


Is it just me, or does there seem to be a css problem at that link?
Actually, looking through the source, the problem page links to /community/forums/theme.asp while a "good" page links to /community/forums/themes/theme1/theme.asp
It happens at other places, such as 2002 Featured Articles

jfl.
datalurkur
datalurkur
your example gives me redefinition errors...
jflanglois
jflanglois
Quote:
Original post by datalurkur
your example gives me redefinition errors...


You're not supposed to define the variable several times. That is, in one cpp file, you will do int my_int = 0;, but in the others, you will just #include the h file with the extern declaration and use my_int.


jfl.
datalurkur
datalurkur
the way I'm trying to fix this is by using

#ifndef INC_SCREEN_H
#define INC_SCREEN_H

//variables defined here

#endif

so technically, shouldn't that work?
jflanglois
jflanglois
Nope it won't work. Here's an example of how you would do it:

// ------- main.cpp --------#include <iostream>#include "extern.h"int main() {  std::cout << a;  func();  std::cout << a;}// ------- extern.h --------#ifndef EXTERN_H#define EXTERN_Hextern int a;void func();#endif// ------- extern.cpp -------#include "extern.h"int a = 0;void func() {  ++a;}


jfl.
datalurkur
datalurkur
Ok, thanks for all your help.
llvllatrix
llvllatrix
Stay away from the extern keyword. Its a very messy way of doing things and imo should be depricated because it tends to produce spaghetti code. Instead write accessor functions; the concept is similiar to the class equivilent, only it applys to a module (*.cpp) instead of a class. Heres an example:

// ------- main.cpp --------#include <iostream>#include "screen.h"void main(void){    // Set the dimentions.    int dimention;    std::cout << "enter width: ";    std::cin >> dimention;    std::cout << endl;    set_width(dimention);    std::cout << "enter height: ";    std::cin >> dimention;    std::cout << endl;    set_height(dimention);    // Echo the dimentions.    std::cout << "Width: " << get_width();    std::cout << "Height: " << get_height();}// ------- screen.h --------#ifndef SCREEN_H#define SCREEN_Hint get_width(void);int get_height(void);void set_width(int width);void set_height(int height);#endif// ------- screen.cpp -------#include "screen.h"static int screen_width = 0;static int screen_height = 0;int get_width(void){    return screen_width;}int get_height(void){    return screen_width;}void set_width(int width){    screen_width = width;}void set_height(int height){    screen_height = height;}


In retrospect this was probably a bad example but the bottom line is you want to provide some layer of abstraction between the variable and the things accessing it. While extern is fairly bareable when used to access variables, it is quite intolerable when it is used to access functions; you have no way of knowing where the function came from short of a grep of tag (which many people dont use). If you want to see extern gone wild, check out the Beowulf source tree.

Hope this helps,
- llvllatrix
Enigma
Enigma
Quote:
Original post by llvllatrix
Stay away from the extern keyword. Its a very messy way of doing things and imo should be depricated because it tends to produce spaghetti code...

static int screen_width = 0;static int screen_height = 0;

Stay away from using the static keyword to indicate local to translation unit. This is deprecated. Use the unnamed namespace instead. Athough in this instance there's no reason to declare the variables local to the translation unit anyway.

Enigma
llvllatrix
llvllatrix
Thx for that enigma; copied old code :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.