Multiple Definition Skill
Fixes linker errors where the same symbol is defined in multiple translation units.
Detection Patterns
multiple definition of 'X'already defined induplicate symbol
Strategy
- Identify Symbol: Extract the duplicated symbol.
- Find Definitions: Locate all places where it's defined.
- Remove Duplicates: Keep one, remove or fix others.
Instructions
Step 1: Parse Error
From: multiple definition of 'globalVar'; first defined in a.o
Step 2: Common Causes & Fixes
Global variable in header:
// Wrong: header.h
int globalVar = 0; // Defined in every .cpp that includes
// Fix Option 1: extern declaration
// header.h
extern int globalVar;
// source.cpp
int globalVar = 0;
// Fix Option 2: inline (C++17)
inline int globalVar = 0;
Function defined in header:
// Wrong
void foo() { ... } // in header
// Fix: make inline
inline void foo() { ... }
Same source compiled twice: Check build file for duplicate source file entries.