Internal and External Linkage

Resolving Multiple Definitions

How do you resolve linker errors related to multiple definitions of the same function?

Abstract art representing computer programming

Linker errors caused by multiple definitions of the same function can be frustrating, but they are essential to address for creating a reliable and maintainable program.

These errors occur when the linker finds more than one definition for a function or variable with external linkage.

Common Causes

  1. Duplicate Definitions: Defining the same function in multiple source files.
  2. Multiple Includes: Including a header file with definitions in multiple source files without proper guards.

Solutions

Use Header Guards: Ensure header files use guards or #pragma once to prevent multiple inclusions.

// myheader.h
#pragma once

void SayHello(); // Declaration only

Separate Declarations and Definitions: Declare functions in header files and define them in source files.

// myheader.h
#pragma once

void SayHello(); // Declaration
// mysource.cpp
#include "myheader.h"
#include <iostream>

void SayHello() {
  std::cout << "Hello, world!\n";
}

Inline Functions: For small functions, consider using the inline keyword in the header file.

// myheader.h
#pragma once

inline void SayHello() {
  std::cout << "Hello, world!\n";
}

Avoid Defining Variables in Headers: Only declare variables in headers, define them in one source file.

// myheader.h
#pragma once

extern int GlobalVar; // Declaration
// mysource.cpp
#include "myheader.h"
int GlobalVar{42}; // Definition

Here’s an example of resolving multiple definitions:

// greeting.h
#pragma once

void SayHello();
// greeting.cpp
#include "greeting.h"
#include <iostream>

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

int main() {
  SayHello();
}

Let’s compile, link and run the program:

g++ greeting.cpp main.cpp -o myProgram
./myProgram
Hello from greeting

By following these practices, you ensure that each function and variable has a single definition, avoiding linker errors and creating a more maintainable codebase.

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