# C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status

**Code:** `multiple definition of` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang, MSVC

The linker found the same function or global variable defined in two object files — usually something defined in a header.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/multiple-definition-of)
