multiple definition of C/C++ Origin: Application & API OSI layer 7 · Application Last updated 15 Oct 2026

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.

Exact message you may see multiple definition offirst defined heremultiple definition of `main'duplicate symbolLNK2005 already defined in
Seen on: C/C++ GCC Clang MSVC

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

  1. Declare with extern int counter; in the header and define it in one .cpp
  2. Mark header functions inline (C++17 also allows inline variables)
  3. Remove the duplicate definition

Detailed fix by platform

C++

  1. inline int counter = 0; (C++17) in a header gives every file the same single variable
  2. Use static or an anonymous namespace for file-local helpers

Code examples

One definition in one place

cpp
// 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 definition

How to diagnose

  1. Symbol — Which name does the linker print?
  2. Files — Which two object files define it?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

cannot find -l — /usr/bin/ld: cannot find -lssl: No such file or directory (C/C++)cannot find -lerror: linker `cc` not found — Rust: error: linker `cc` not found — note: No such file or directory (os error 2) (Rust)error: linker `cc` no…file in wrong format — ld: file in wrong format / skipping incompatible libfoo.so when searching (C/C++)file in wrong formatimplicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …incomplete type — C++: error: invalid use of incomplete type 'class X' — forward declaration only (C/C++)incomplete typeLNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104LNK2005 — MSVC: error LNK2005: already defined in main.obj / LNK1169: one or more multiply defined symbols found (C/C++)LNK2005recompile with -fPIC — relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC (C/C++)recompile with -fPICmultiple …

Explore the map of this error Full error map →

🧠 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.