extern

Started by
4 comments, last by EternalOblivion 19 years, 10 months ago
what is it? what is it used for? whats its syntax?
Advertisement
google?

what is it? what is it used for? whats its syntax?

--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net''s DirectX FAQ. (not as cool as the Graphics and Theory FAQ)
it prevents a declaration from being also defined at the same time it basically says your going to define it some where else, i think function prototypes are implicitly flagged with extern then you define them in another file, prevents multiple definitions that can cause bugs.

Mainly used in declaring global variables that will be used in multiple files like the global stream objects in c++.
Read this article, which explains not just the extern keyword, but the implications of #include and how to properly organize symbols, entities and files in C and C++.
thank you for the help.
Here is a explanation / example for REAL people on what extern can do

(This is in straight up C)
Say your making a big game and it needs to be busted up into several files and compiled together into one EXE. So for example you have your main program in the source file Engine.cpp and your main chatacter code in a file called MainDude.cpp.

Now inside of the Engine.cpp you have a global variable for your score. You make it global because the odds are lots of other pieces of code will want to increment or decrament it, and it is used far to much to be passed around. So in the top of Engine.cpp you have this

int nGameScore;

Now lets say that a piece of code inside of your main character code needs to mess with this variable. Well it is global in Engine.cpp so anyone should be able to access it, and they can. Well the problem is that the compiler goes to compile MainDude.cpp and sees something like nGameScore++; Well you didn''t declare a variable in that file by that name and it doesn''t know about the global one you declared inside Engine.cpp so it freaks out and says it is an undeclared identifier.

Well here is how you use extern and tell the compiler that it was declared already, just in another file, and that it should shut up and leave you alone :D

Remember in the top of Engine.cpp we have this:
int nGameScore;

So to tell the compiler about this global in the other file put this (at the top of MainDude.cpp):
extern int nGameScore;

This way when the compiler hits that line in MainDude.cpp nGameScore++; It says "Oh, yeah he told me that this variable was declared already external to this file" and it shuts up and leaves you alone.

That is a quick example without all the technical book babble crap of how to use it :D

Hope this helped you out man. If you have anymore questions, feel free to bug me on our own forum



Sincerely,
Randy Trulson
www.GamePotato.com
www.NeuronGames.com
Sincerely,Randy Trulsonwww.NeuronGames.com

This topic is closed to new replies.

Advertisement