C++ – Scope and Linkage

Let’s see how the compilation process works in C++.

The compilation process is carried out for each source file separately. First, the pre-processor resolves the #include directives and appends the code from these included files to the source file.  This bigger file is called as a translation unit. Compiler considers the translation unit as a basic unit and compiles it to create the object file. Similarly, object files are created for the other translation units in the project as well. Once all the object files have been created, the linker links the object files to create an executable file.

A variable in C++ can be thought to have two main properties, scope and linkage. Scopes are areas within which a variable can be accessed. Although, there are around five types of “official” scopes in C++, following are the most important at least according to me.

Local Scope (Block Scope) – A variable declared within a block  is only accessible within the block. A block can be considered as the area within two curly braces (“{” and “}”).  Some of these variables might exist in memory until the end of the program (static variable, for example) but they will never be accessible outside of the block where they are declared. Please note that the scope starts from the line where the variable is declared up to the closing brace (“}”).

Global Scope/ Global Namespace Scope / File Scope –  A variable declared outside of a block falls under this category. These types of variables are accessible throughout the file where they are declared. The scope of a global variable actually extends to the entire translation unit. As we discussed before, a translation unit is nothing but a larger source file after the pre-processor has added the included files into the original file.  Note that even global variables are not directly accessible to other translation units that might be part of the same project. It is when you want to access a global variable declared in one translation unit in another translation unit that you use the linkage property of these variables.

A non-constant global variable has external linkage by default.  It means that you will be able to use this variable in other translation units just by providing a forward declaration. If you want to make this variable unavailable in other translation units, it can be declared as a static variable. A static global variable will not be visible in other translation units. A constant global variable has internal linkage by default. This means that it will not be available in other translation units. You can add the keyword ‘extern’ to the front of this variable to give it an external linkage and make it available in other translation units. To summarize,

int var1; //has external linkage by default.
static int var2; // explicit change to internal linkage.
const int var3; // internal linkage by default.
extern const int var4; // explicit change to external linkage

Please note that if you need to access a variable (with external linkage) declared in the file ‘a.cpp‘ in another file ‘b.cpp‘,  you will need to provide a forward declaration of the variable in ‘b.cpp‘. The scope of this variable in ‘b.cpp‘ depends on where you specify the forward declaration within the file.  A forward declaration looks like the following

extern int var1;

The same keyword ‘extern’ is used for forward declaration as well as to explicitly provide external linkage to const variables. Be sure to understand the two use-cases.

One thought on “C++ – Scope and Linkage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s