Archived

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

wah_on_2

What mean of extern?

Recommended Posts

Guest Anonymous Poster
you normally use extern in header files.


example: // this would be in game.h
extern int health; // can''t initialize only declare
// this allows you to use in any code or source file
// within that program think of it as a "super-global"

/*in game.c or game.cpp*/
int health = 100; // here is where you initialize it
// basically you can initialize it anywhere outside the
// header file it was declared in


extern "C" allows you to use C-specific functions/code in C++ files ... sorry don''t have an example

--Alpha

Share this post


Link to post
Share on other sites
simply...

extern int a;
means there is a variable called a declared somewhere in my source files... that variable should be global and shouldn''t be a static variable...

-----------------------------

DaHeR.RailgunMaster

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

extern "C" allows you to use C-specific functions/code in C++ files ... sorry don''t have an example




an example for that
  
extern "C" void C_Code();

// and the most common

extern "C" {
void C_Code0();
void C_Code1();
}





-----------------------------

DaHeR.RailgunMaster

Share this post


Link to post
Share on other sites
quote:
Original post by wah_on_2
I found that some c/c++ programs using extern keyword. What is this?

It''s a directive telling the compiler that the definition of the function or variable will be found outside of the current translation unit and, therefore, will be fixed-up by the linker.
quote:

And when/where will we use extern?

Anytime its undesirable to define the entity within the current translation unit, such as when you link to library functions.
quote:

Also, is extern "c" == extern? What different?

They''re similar, but extern "C" tells the compiler to apply the local C linkage convention to entities that the extern applies to. Without the "C", the compiler will default to C++ linkage conventions. This protocol enables linking of declarations compiled using a C++ compiler with binaries compiled with a C compiler. Furthermore, the C and C++ standards allow vendors to extend the convention to link to binaries written using other languages, and you might see things such as extern "FORTRAN" or extern "PASCAL" in some environments.

Share this post


Link to post
Share on other sites