# C/C++: undefined reference to `main' — (.text+0x20) in crt1.o

**Code:** `undefined reference to main` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang, MSVC

The linker has no `main` function: the file with `main` is not compiled or linked, or `main` is spelled or declared incorrectly.

## Meaning

Every executable needs exactly one `main`. The message points into `crt1.o` (the startup code) because that code calls `main`. Compiling only a library file, a misspelled `Main`, or building a library as an executable all produce it.

## Common causes

- Building with a source file that has no `main()` (library code only)
- `main` misspelled or has the wrong signature (`void main` in C++ is nonstandard)
- Wrong source file listed in the command or CMake target
- `main` is inside a namespace or an `#ifdef` that is not active

## Quick fix

1. Compile the file that contains `main`
2. Use `int main()` or `int main(int argc, char** argv)`
3. Use `add_library` only for libraries and `add_executable` for programs

## Detailed fix by platform

### GCC / Clang

1. `g++ main.cpp util.cpp -o app` — list every source file, including the one with `main`
2. Use `-c` only when you want object files and then link them in a second step

## Code examples

### Compile and link everything

```bash
# wrong: compiles only util.cpp, which has no main
g++ util.cpp -o app
# /usr/bin/ld: crt1.o: in function `_start': (.text+0x20): undefined reference to `main'

# right
g++ main.cpp util.cpp -o app
```

## How to diagnose

1. **Source** — Is a file with main in the command?
2. **Name** — Exactly main (lowercase)?
3. **Target** — Executable or library?

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