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.
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.
Answers to questions are automatically generated and may not have been reviewed.
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
Comprehensive course covering advanced concepts, and how to use them on large-scale projects.
View Course