C++ multiple definition Error — Complete Guide
이 글의 핵심
C++ multiple definition error C++, multiple, definition, Introduction: "Compiles but multiple definition at link..." explained in detail with practical examples.
Introduction: “Compiles but multiple definition at link…"
"Defined function in header file and got error”
When splitting C++ project into multiple files, you encounter multiple definition of error. Compilation succeeds but fails at link stage.
// utils.h
void foo() { // ❌ Definition in header
std::cout << "foo\n";
}
// main.cpp
#include "utils.h"
// other.cpp
#include "utils.h"
// Link error:
// multiple definition of 'foo()'
// first defined here: main.o
// also defined here: other.o
What This Guide Covers:
- Why multiple definition error occurs
- Understanding ODR (One Definition Rule)
- 5 major causes and solutions
- Header file writing rules
- Difference between inline, static, extern
1. What is multiple definition Error?
Error Message
Here is a simple text code example. Ensure stability through error handling. Try running the code directly to check its operation.
/usr/bin/ld: other.o: in function `foo()':
other.cpp:(.text+0x0): multiple definition of `foo()';
main.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Meaning: foo() function is defined in both main.o and other.o, linker does not know which to use.
2. ODR (One Definition Rule)
Rule
C++ standard’s ODR (One Definition Rule):
- Variable/function must be defined at most once in entire program
- Class/template/inline function can be defined multiple times (but all must be identical)
ODR Violation Example
// ❌ ODR violation
// file1.cpp
int globalVar = 42;
// file2.cpp
int globalVar = 99; // ❌ Duplicate definition
// Link error: multiple definition of 'globalVar'
ODR Compliant Example
// ✅ ODR compliant
// header.h
extern int globalVar; // Declaration only
// file1.cpp
int globalVar = 42; // Definition (in one place only)
// file2.cpp
#include "header.h"
// Can use globalVar (no definition)
3. Five Major Causes and Solutions
Cause 1: Function Definition in Header
Most common cause: Defining regular function in header file.
// ❌ utils.h
void foo() { // Definition in header
std::cout << "foo\n";
}
// main.cpp
#include "utils.h"
// other.cpp
#include "utils.h"
// Link error: multiple definition of 'foo()'
Solution 1: Separate Declaration and Definition (Recommended)
// ✅ utils.h
void foo(); // Declaration only
// utils.cpp
void foo() { // Definition
std::cout << "foo\n";
}
Solution 2: inline Keyword
Here is a simple C++ code example. Try running the code directly to check its operation.
// ✅ utils.h
inline void foo() { // inline: allows multiple definitions
std::cout << "foo\n";
}
Solution 3: static Keyword (Not Recommended)
Here is a simple C++ code example. Try running the code directly to check its operation.
// ✅ utils.h
static void foo() { // Separate copy for each .cpp
std::cout << "foo\n";
}
Caution: static creates separate function for each .cpp file, increasing code size.
Cause 2: Global Variable Definition in Header
// ❌ config.h
int maxConnections = 100; // Definition in header
// main.cpp
#include "config.h"
// server.cpp
#include "config.h"
// Link error: multiple definition of 'maxConnections'
Solution 1: Use extern (Recommended)
// ✅ config.h
extern int maxConnections; // Declaration only
// config.cpp
int maxConnections = 100; // Definition (in one place only)
Solution 2: inline Variable (C++17)
Here is a simple C++ code example. Try running the code directly to check its operation.
// ✅ config.h (C++17)
inline int maxConnections = 100; // inline variable
Summary
Key Points
- ODR: One Definition Rule - define at most once
- Header files: Declaration only, definition in .cpp
- inline: Allows multiple definitions
- extern: Declaration, definition elsewhere
- static: Separate copy per file (not recommended)
When to Use
✅ Use inline when:
- Small functions in headers
- Template functions
- constexpr functions
✅ Use extern when:
- Global variables
- Sharing variables across files
❌ Don’t use:
- Regular function definitions in headers
- Global variable definitions in headers
Best Practices
- ✅ Separate declaration and definition
- ✅ Use inline for small functions
- ✅ Use extern for global variables
- ❌ Don’t define functions in headers
- ❌ Don’t use static for functions (increases code size)
Related Articles
Master ODR for clean C++ code! 🚀
Frequently Asked Questions (FAQ)
Q. When would I use this in practice?
A. Everything about C++ multiple definition Error : from basic concepts to practical applications.
Q. What should I read before this?
A. Follow the previous article or related articles links at the bottom of each post to learn in sequence. See the C++ series index for the full picture.
Q. Where can I study this more deeply?
A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.
Related Articles (Internal Links)
Other articles related to this topic.
- C++ LNK2019 | ‘unresolved external symbol’ 링커 에러 원인 5가지와 해결법
- C++ One Definition Rule | ‘단일 정의 규칙’ 가이드
- C++ Header Files | ‘헤더 파일’ 가이드
Keywords Covered in This Article (Related Search Terms)
This article covers C++, linker-error, multiple-definition, ODR, header-guard, inline, duplicate-definition.