# C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status

**Code:** `undefined reference to` · **Category:** C/C++ · **Seen on:** C/C++

The linker can’t find the definition of a function or variable that was declared and used.

## Meaning

The code compiled, but the object file or library containing the implementation wasn’t linked: a missing .cpp in the build, a missing -l library (or wrong order), C vs C++ name mangling, or a template defined only in a .cpp.

## Common causes

- Source file defining the function not compiled/linked
- Library not linked or listed before the objects (-lfoo order)
- C function used from C++ without extern "C"
- Declaration and definition signatures differ

## Quick fix

1. Add the .cpp/.c to the build (CMake target_sources)
2. Link the library after your objects (-lm, -lpthread, target_link_libraries)
3. Wrap C headers in extern "C"

## Detailed fix by platform

### CMake

1. 

```cpp
add_executable(app main.cpp utils.cpp)
target_link_libraries(app PRIVATE m pthread)
```


## How to diagnose

1. **Symbol** — Which name? (c++filt to demangle)
2. **Build** — All sources compiled?
3. **Libraries** — Linked and order

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/undefined-reference-to)
