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
- Duplicate Definitions: Defining the same function in multiple source files.
- 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