What mean of extern?

Started by
4 comments, last by wah_on_2 21 years, 5 months ago
I found that some c/c++ programs using extern keyword. What is this? And when/where will we use extern? Also, is extern "c" == extern? What different? Thx
Advertisement
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
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
____________________________________MSN | AIM | YIM | ICQ
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 commonextern "C" {void C_Code0();void C_Code1();}  





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

DaHeR.RailgunMaster
____________________________________MSN | AIM | YIM | ICQ
lol... thanks!

--Alpha
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.

This topic is closed to new replies.

Advertisement