Resolving Multiple Definitions

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

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.

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?
Using Anonymous Namespaces
How do anonymous namespaces affect linkage and scope?
Using the extern Keyword
Can you give more examples of when to use the extern keyword?
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