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.
Here is detailed implementation code using C++. Import the necessary modules. Understand the role of each part while examining the code.
// 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
Below is an implementation example using C++. Try running the code directly to check its operation.
// ❌ ODR violation
// file1.cpp
int globalVar = 42;
// file2.cpp
int globalVar = 99; // ❌ Duplicate definition
// Link error: multiple definition of 'globalVar'
ODR Compliant Example
Below is an implementation example using C++. Import the necessary modules. Understand the role of each part while examining the code.
// ✅ 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.
Below is an implementation example using C++. Import the necessary modules. Understand the role of each part while examining the code.
// ❌ 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)
Below is an implementation example using C++. Try running the code directly to check its operation.
// ✅ 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
Below is an implementation example using C++. Import the necessary modules. Understand the role of each part while examining the code.
// ❌ 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)
Below is an implementation example using C++. Try running the code directly to check its operation.
// ✅ 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! 🚀
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Everything about C++ multiple definition Error : from basic concepts to practical applications. Master key content quick… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ LNK2019 | ‘unresolved external symbol’ 링커 에러 원인 5가지와 해결법
- C++ One Definition Rule | ‘단일 정의 규칙’ 가이드
- C++ Header Files | ‘헤더 파일’ 가이드
이 글에서 다루는 키워드 (관련 검색어)
C++, linker-error, multiple-definition, ODR, header-guard, inline, duplicate-definition 등으로 검색하시면 이 글이 도움이 됩니다.