undefined reference to main C/C++ Origin: Application & API OSI layer 7 · Application 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):

C compiler cannot create executables — configure: error: C compiler cannot create executables / no acceptable C compiler found in $PATH (C/C++)C compiler cannot cre…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…does not appear to contain CMakeLists.txt — CMake Error: The source directory does not appear to contain CMakeLists.txt (C/C++)does not appear to co…error: linker `cc` not found — Rust: error: linker `cc` not found — note: No such file or directory (os error 2) (Rust)error: linker `cc` no…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 …Killed signal terminated program cc1plus — g++: fatal error: Killed signal terminated program cc1plus — compilation terminated (out of memory) (C/C++)Killed signal termina…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 separatorundefined…

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.