multiple definition of C/C++ Origin: Application & API 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):

implicit 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++)LNK1104redefinition of — C++: error: redefinition of 'class X' / multiple definition — missing include guard (C/C++)redefinition ofundefined reference to — C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status (C/C++)undefined reference toundefined reference to main — C/C++: undefined reference to `main' — (.text+0x20) in crt1.o (C/C++)undefined reference t…undefined reference to pthread_create — C/C++: undefined reference to `pthread_create' — missing -pthread (also `dlopen`, `clock_gettime`) (C/C++)undefined reference t…undefined reference to vtable — C++: undefined reference to `vtable for X' — virtual function not defined (C/C++)undefined reference t…multiple …

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.