undefined reference to main C/C++ Origin: Application & API Last updated 15 Oct 2026

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

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

Exact message you may see undefined reference to `main'(.text+0x20): undefined reference to `main'crt1.o: in function `_start'entry point must be definedLNK2001 unresolved external symbol main
Seen on: C/C++ GCC Clang MSVC

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?

🔧 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):

Could not find a package configuration file — CMake Error: Could not find a package configuration file provided by "Foo" (requested version 1.2) (C/C++)Could not find a pack…fatal error: No such file or directory — C/C++: fatal error: foo.h: No such file or directory — #include not found (C/C++)fatal error: No such …LNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104missing separator — Make: Makefile:5: *** missing separator. Stop. (spaces instead of a tab) (C/C++)missing separatormultiple definition of — C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status (C/C++)multiple definition ofNo rule to make target — Make: *** No rule to make target 'foo.c', needed by 'foo.o'. Stop. (C/C++)No rule to make targetundefined reference to — C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status (C/C++)undefined reference toundefined reference to pthread_create — C/C++: undefined reference to `pthread_create' — missing -pthread (also `dlopen`, `clock_gettime`) (C/C++)undefined reference t…undefined…

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.