Using the extern Keyword

Can you give more examples of when to use the extern keyword?

The extern keyword in C++ is used to declare a variable or function that is defined in another translation unit.

This is particularly useful when working with global variables and functions that need to be accessed across multiple files.

Example of extern with Variables

The following simple program shows the extern keyword in action:

// globals.cpp
int GlobalVar{42};
// main.cpp
#include <iostream>

extern int GlobalVar;

int main() {
  std::cout << "GlobalVar: " << GlobalVar;
}
GlobalVar: 42

Explanation

  • In globals.cpp, GlobalVar is defined.
  • In main.cpp, extern int GlobalVar; declares that GlobalVar exists and will be defined elsewhere.
  • This allows GlobalVar to be used in main.cpp without defining it there.

Example of extern with Functions

Here's an example with an extern function:

// greeting.cpp
#include <iostream>

void SayHello() {
  std::cout << "Hello from greeting\n";
}
// main.cpp
#include <iostream>

extern void SayHello();

int main() {
  SayHello();
}

Explanation

  • In greeting.cpp, SayHello() is defined.
  • In main.cpp, extern void SayHello(); declares that SayHello() exists elsewhere.
  • This allows SayHello() to be called in main.cpp.

Benefits of extern

  1. Code Organization: It helps keep code modular and organized by allowing definitions to be separated from declarations.
  2. Global Variables: Facilitates the use of global variables across different files without redefining them.

Common Mistakes

  1. Multiple Definitions: Ensure variables defined with extern are only defined once across the entire program.
  2. Matching Declarations: The extern declaration must match the definition in terms of type and qualifiers.

Example of extern Constants

The extern keyword can be combined with other qualifiers, such as const:

// math.cpp
extern const float Pi{3.14159f};
// main.cpp
#include <iostream>

extern const float Pi;

int main() {
  std::cout << "Pi: " << Pi;
}
Pi: 3.14159

In this case, Pi is a const variable with extern linkage. The extern keyword allows its use in main.cpp, even though it is defined in math.cpp.

The extern keyword is essential for managing linkage and ensuring variables and functions can be accessed across multiple files, promoting a modular and organized code structure.

Internal and External Linkage

A deeper look at the C++ linker and how it interacts with our variables and functions. We also cover how we can change those interactions, using the extern and inline keywords

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Understanding Object Files
Can you explain the concept of object files in more detail?
Resolving Multiple Definitions
How do you resolve linker errors related to multiple definitions of the same function?
Using Anonymous Namespaces
How do anonymous namespaces affect linkage and scope?
Managing Global Variables
What are some best practices for managing global variables in large projects?
Using the Inline Keyword
How does the inline keyword help with the one-definition rule?
Linkage of Constants
Why do const and constexpr variables have internal linkage by default?
Using Inline Variables
What are the advantages of using inline variables over other methods to avoid multiple definitions?
Scope Resolution Operator
How do you use the scope resolution operator to access shadowed symbols?
Extern Constexpr in Visual Studio
Can you explain the /Zc:externConstexpr option in Visual Studio?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant