Internal and External Linkage

Using the extern Keyword

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

Abstract art representing computer programming

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.

This Question is from the Lesson:

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved