The extern keyword in C++ is used to share variables and functions among various source files to produce modular and maintainable code. By declaring a variable as extern, the compiler comes to know that the definition of the variable is done somewhere else to prevent redundant definitions. In this article, we will discuss the extern keyword, its syntax, and the best ways of using the extern for sharing variables between source files in C++, along with some of its best practices.
Table of Contents:
What is the extern Keyword in C++?
The keyword extern is a function or variable that is declared in another source file. This declaration instructs the compiler about the existence of that variable or function to avoid unnecessary duplication and to facilitate cross-file sharing.
How Does ‘extern’ Work?
When a variable is declared as an extern type, the memory for it is not allocated, instead, the reference is made to one variable that is defined somewhere else. It is the job of the linker to make sure that one file’s declaration is linked to another file’s definition correctly.
Syntax for Declaring an Extern Variable
extern data_type variable_name;
Example:
Output:
The code shows the use of an extern keyword by declaring functions before defining them and how it modifies and prints a shared global variable.
Using ‘extern’ to Share Variables among Source Files
Below is a multi-source C++ program using extern to show you how to share variables between multiple files.
This file contains declarations of the shared variable and functions.
2. Create file1.cpp (Variable Definition & Function Implementation)
3. Create file2.cpp (Modify the Shared Variable)
4. Create the main.cpp (Main File)
5. Compile and Run
Use a C++ compiler like g++ to compile and link all files together by using the code given below:
g++ main.cpp file1.cpp file2.cpp -o program
./program # Run the program
Output:
The code shows how to use extern to share variables between source files in a few steps procedure.
Use Cases of extern Keyword in C++
- To share global variables across various files without declaring them redundantly.
- For using functions in a header file for the multiple usages of .cpp files.
- The const variables have internal linkage by default, so extern const is needed if you want to use a const variable across multiple files.
- Linking functions written in C into C++ source files through extern “C” so that name mangling doesn’t occur.
- To define a global object in one file that will be accessed in another.
- For memory saving by not duplicating variable definitions.
- Supports modular programming by separating declarations and definitions.
- For providing external linkage for global functions and variables.
Best Practices for Using extern in C++
- Declare the extern variable in the header and define it in a .cpp file to avoid more than one definition.
- As const variables by default have internal linkage, use extern const for passing them between files.
- Declare the functions in headers (functions are extern by default).
- Using extern “C” sorts out the interoperability issues of C/C++ by preventing name mangling.
- Do not ever declare local variables as extern, only global variables can be declared.
- Don’t use global variables, prefer namespaces for better efficiency.
- As far as internal linkage is concerned, whenever needed, you can go for static instead of extern because extern provides external linkage and they are not interchangeable.
- For better maintainability, limit your global variables.
- You can use Singletons instead of global objects for better encapsulation.
- Wrap the extern declarations with header guards to protect against redefinition errors.
Conclusion
‘extern’ is an important mechanism that can be used to share functions and variables between many C++ source files. Proper use enhances modularity and avoids the unnecessary allocation of memory. On the other hand, improper use leads to multiple definition errors or unintended modifications. Thus, understanding best practices for extern and uses will help in gaining better knowledge in C++.
FAQs
1. What is the purpose of an extern keyword in C++?
The extern keyword is a specifier that is used to declare a variable or function that is defined in another source file such that cross-file access does not engage in multiple definitions.
2. Does extern allocate memory for the variable?
Memory is allocated where the variable is defined, not necessarily on a single line.
3. Can I use extern with const variables?
The const variables have an internal linkage by default. So, you use extern const to ensure sharing between files.
4. What happens if I define a global variable in multiple files without extern?
You will get a linker error about multiple definitions of that variable.
5. Can extern be used with local variables?
No, extern can be declared only for global variables. It will throw an error during compilation if used inside the function.