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.
undefined reference to `main'(.text+0x20): undefined reference to `main'crt1.o: in function `_start'entry point must be definedLNK2001 unresolved external symbol main 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) mainmisspelled or has the wrong signature (void mainin C++ is nonstandard)- Wrong source file listed in the command or CMake target
mainis inside a namespace or an#ifdefthat is not active
⚡ Quick fix
- Compile the file that contains
main - Use
int main()orint main(int argc, char** argv) - Use
add_libraryonly for libraries andadd_executablefor programs
Detailed fix by platform
GCC / Clang
g++ main.cpp util.cpp -o app— list every source file, including the one withmain- Use
-conly when you want object files and then link them in a second step
Code examples
Compile and link everything
# 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 appHow to diagnose
- Source — Is a file with main in the command?
- Name — Exactly main (lowercase)?
- 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:
Similar errors
- Could not find a package configuration file CMake Error: Could not find a package configuration file provided by "Foo" (requested version 1.2)
- fatal error: No such file or directory C/C++: fatal error: foo.h: No such file or directory — #include not found
- LNK1104 MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running)
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026