Asked in
Computer Programming
C Programming

What are external variables?


User Avatar
Wiki User
August 30, 2007 10:44AM

The concept of external variable has different meanings from language to language. The primary context in which the phrase is used is in the C and C++ programming languages. The concept is commonly incorrectly explained on the web. Please refer to a real c++ programming text for finality. In C and C++, keyword extern means either of two things, dependent on context. When applied to the declaration of a variable , extern tells the compiler that said variable will be found in the symbol table of a different translation unit. This is used to expose variables in one implementation file to a different implementation file. It is important to understand that the external declaration is not resolved by the compiler, but instead by the linker, meaning that extern is a legitimate mechanism to install modularity at the compiler level; if, for example, you create a debug and a release version of a library , you can switch between the two simply by loading one or the other, without recompiling either library or the things dependent thereupon. When applied to the instantation of a variable, extern promotes that variable into the symbol table of the translation unit. This is frequently misunderstood to mean that the variable is global; this is incorrect. The variable must already be global for externality to make sense. What this actually means is that the symbol may be referenced from other translation units. There must be a single implementation of the variable somewhere, marked extern (typically in the header,) for other translation units to refer to the variable. Unfortunately, externality is frequently misrepresented as an issue of scope; scope has nothing to do with externality. Scope is an issue of symbol availability and symbol conflict resolution not terribly unlike namespaces. Externs are always global, and thus scope is essentially a foreign concept. Never the twain shall meet. It is incorrect to say that variables in global scope must be externed within the scope of any function that depends upon those globals. Two important uses of externality are common: preventing circular references (in a fashion similar to the section declarations of languages like Pascal) and preventing compiled dependency chains. In the latter case, consider the example of a video game with a large amount of graphic resources. If the game code is portable, the author cannot rely on compiler extensions like binary inclusion, and therefore must convert their resources to arrays and compile them into the application from source. Because those resources are physically huge, compiling them can take a long time. If these resources are kept each in their own translation unit, however, and referenced as extern from other TUs, then when those other TUs are recompiled, the graphics are just re-linked, meaning they only need to be compiled the once. The effect described above can serve a similarly important role in breaking the recompile chain which otherwise would form from a huge stack of inclusions, by keeping things in separate TUs and therefore modular at the linker level. The more granular such distinctions are, the further compile times drop, and the lower dependencies between modules become; as such, extern is a tremendously important yet rather subtle tool for the modern C/C++ developer.