Jump to content
  • Advertisement

Archived

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

EternalOblivion

extern

This topic is 5228 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

Advertisement
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++.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!