C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status
The linker found the same function or global variable defined in two object files — usually something defined in a header.
multiple definition offirst defined heremultiple definition of `main'duplicate symbolLNK2005 already defined in Meaning
Each .cpp is compiled separately; if a header contains int counter = 0; or a non-inline function body, every file that includes it gets its own copy and the linker refuses to merge them. Declare in the header, define once in a .cpp, or mark it inline / static / constexpr.
Common causes
- Variable or function defined (not declared) in a header included by several .cpp files
- Two .cpp files define the same function
- Including a .cpp file in another one that is also compiled
- Same library linked twice
⚡ Quick fix
- Declare with
extern int counter;in the header and define it in one .cpp - Mark header functions
inline(C++17 also allowsinlinevariables) - Remove the duplicate definition
Detailed fix by platform
C++
inline int counter = 0;(C++17) in a header gives every file the same single variable- Use
staticor an anonymous namespace for file-local helpers
Code examples
One definition in one place
// config.h
#pragma once
extern int g_retries; // declaration only
inline int clamp0(int v) { return v < 0 ? 0 : v; } // inline: allowed in headers
// config.cpp
#include "config.h"
int g_retries = 3; // the single definitionHow to diagnose
- Symbol — Which name does the linker print?
- Files — Which two object files define it?
- Header — Is it defined in a header?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- implicit declaration of function C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration]
- incomplete type C++: error: invalid use of incomplete type 'class X' — forward declaration only
- LNK1104 MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running)
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026