C++

C++ Undefined Reference to Main Fix

Every file in the program can compile cleanly and the build still fails at the last step, because the startup code needs one symbol - a function named main in the global namespace - and none of the files handed to the linker provided it. This guide separates the five shapes that produce that failure, reads what the linker diagnostic actually names, and matches each shape to its repair: define main, move it to the global namespace, or spell it the way the startup code looks it up.

Written November 24, 2026 · Tested with GCC 13.3.0 · 8 min read

By the end of this guide
  • The rule underneath: a program shall contain a global function named main
  • How to read the diagnostic
  • The first failure: no main at all
  • The second failure: main inside a namespace

Written November 24, 2026

Tested with GCC 13.3.0

What this guide is

A C++ program can pass every compile step and still not become a program. The compiler checks each file on its own; the linker then collects the compiled files, and the startup code that runs before any of your code needs to call one function: main, in the global namespace, returning int. When no definition of that function is among the files being linked, the linker stops with "undefined reference to `main'" - and it does not matter how the definition went missing.

The five forms in this guide each reproduce the failure with a small, complete program, and they go missing five different ways: there is no main at all, main is defined inside a namespace, the function is spelled Main with a capital letter, main is declared but never defined, and the only main is a static member function of a class. In each family the failing program is small, the full linker output is shown, and the fix supplies the one definition the startup code is looking for while keeping the rest of the program as it was. Five neighboring shapes in the traps are not this error: a main with no return statement compiles and exits with status 0, the two-parameter form of main compiles and receives the command-line arguments, a main defined in a second file satisfies the linker when both files are linked together, and two further shapes - void main() and a free-standing static int main() - are rejected by the compiler itself, earlier, with different errors.

Build every program in this guide with g++ -std=c++20. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: a program shall contain a global function named main

The main function page states the requirement directly: a program shall contain a global function named main, which is the designated start of the program in a hosted environment. Global is the load-bearing word. The function must be in the global namespace - a function named main inside a namespace, or inside a class, is a different function that happens to share the name, and the page notes the name main can be used for entities in a non-global namespace without providing the program's entry point.

The page gives the forms main may take. It returns int, and it takes either no parameters - int main() - or two - int main(int argc, char* argv[]), where the parameter names are arbitrary and char** may replace char* argv[]. Since C++17 the same two forms with noexcept are also required to be allowed. Other forms exist only where an implementation chooses to allow them, as long as they return int.

main also carries restrictions no ordinary function has. It cannot be overloaded, and it cannot be declared with the static specifier - the traps show what GCC does with a free-standing static int main(). Its return type cannot be deduced. And it has one privilege no ordinary function has: its body does not need a return statement, because reaching the end of main without one has the effect of executing return 0; - the first trap is that rule at work.

None of these rules is checked by looking at one file in isolation, which is why the failure arrives where it does. A declaration of main in a file is a promise the compiler accepts; a definition in the wrong namespace is a definition of some other function, which the compiler also accepts. The check that fails is the linker's: the startup object, Scrt1.o, contains a call from its _start function to the symbol main, and linking is the step that must pair every referenced symbol with a definition. The GNU linker documentation describes the entry point as the first instruction executed in a program; on this toolchain that entry point is _start, and _start is where the reference to main comes from.

How to read the diagnostic

Every failure in the five families prints the same three lines, and none of them names a line of your code:

Output
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

Read them in order. The first line names the object that contains the unsatisfied reference: Scrt1.o, the C runtime startup object, in its function _start. That is the code that runs first and calls main to start the program. The second line is the failure itself: at offset 0x1b in that function, the symbol main is referenced, and no definition of it was found in anything being linked. The third line is the build driver's summary - collect2 reporting that the linker, ld, exited with status 1 - not a second error.

Three practical consequences follow. First, there is exactly one error in each transcript, however broken the program's entry point is: the linker reports the missing symbol once and stops. Second, the absence of a file-and-line from your own code is itself the information - there is no definition to point at, so the diagnostic cannot point at one; searching your files for the spelling, scope, and declaration-versus-definition state of main is the debugging step, and the five families are the five places that search can land. Third, the symbol name is exact: main, lower case, in the global namespace. A definition that differs in spelling, in scope, or in existing only as a declaration leaves the symbol undefined in exactly the same way, which is why all five families produce byte-for-byte the same three lines.

The first failure: no main at all

A program with a helper function and nothing else:

C++
#include <iostream>

int helper() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fail_no_main fail_no_main.cpp
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

The file compiles: helper is a valid function, correctly defined. But nothing in the file - or in anything else being linked - defines main, so the startup code's reference is unsatisfied. This is the plain form of the error, and it is the form that appears when a file of library-style functions is compiled as though it were a program, or when the file that held main is left out of the build command.

The fix adds the definition, and lets it call the helper that was already there:

C++
#include <iostream>

int helper() {
    std::cout << "ready\n";
    return 0;
}

int main() {
    return helper();
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fix_no_main fix_no_main.cpp
$ ./fix_no_main
ready

The output is ready, printed by the helper, and the program returns the helper's value as its exit status. Note what the fix did not need: no change to the helper, no header, no special flag. The missing piece was the definition itself. When this error appears in a larger build, the equivalent check is whether the file that defines main is actually in the list of files handed to the compiler - a definition in a file that is never compiled and linked satisfies nothing.

The second failure: main inside a namespace

A main defined inside a namespace:

C++
#include <iostream>

namespace app {

int main() {
    std::cout << "ready\n";
    return 0;
}

}  // namespace app
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fail_main_in_namespace fail_main_in_namespace.cpp
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

This program does define a function named main - app::main. That is a different function from ::main. The requirement is for a global function named main, and a name declared inside a namespace belongs to that namespace, so the global symbol the startup code references is still undefined. The compiler accepts the definition without complaint, because a function named main inside a namespace is legal - it simply is not the program's entry point, and nothing will ever call it automatically.

The fix moves the definition to the global namespace:

C++
#include <iostream>

int main() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fix_main_in_namespace fix_main_in_namespace.cpp
$ ./fix_main_in_namespace
ready

The output is ready. If the namespaced function holds logic worth keeping, the shape that works is the fifth family's fix in miniature: keep the namespaced function under a name of its own, and write a global main that calls it. What does not work is a using-declaration or a call from somewhere else in the file - the symbol the linker needs is a definition of main at global scope, and only writing one there provides it.

The third failure: Main with a capital letter

A function spelled Main instead of main:

C++
#include <iostream>

int Main() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fail_capital_main fail_capital_main.cpp
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

C++ names are case-sensitive, and the symbol the startup code references is main, all lower case. Main is an ordinary function with a similar name: it compiles, it could be called from other code, and it will never be found by the startup code. This family is easy to produce by habit from languages and conventions where the entry point is spelled with a capital, and easy to miss when reading, because the program looks complete.

The fix is the spelling:

C++
#include <iostream>

int main() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fix_capital_main fix_capital_main.cpp
$ ./fix_capital_main
ready

The output is ready. The same exactness applies to the rest of the signature: the traps show a void return and a static specifier each failing in their own way. When the linker reports main undefined and a definition appears to be present, read the definition's name character by character before reading anything else - this family and the previous one are both cases of a definition that is present but is not the definition being looked for.

The fourth failure: main declared but never defined

A declaration of main, a defined helper, and no definition of main:

C++
#include <iostream>

int main();

int helper() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fail_main_declared_only fail_main_declared_only.cpp
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

A declaration - int main(); - states that a function exists somewhere. The compiler works on that promise: code in the file can be compiled against the declaration, and this file compiles cleanly. But a declaration defines nothing and provides no symbol, so when the linker goes looking for the definition the startup code references, there is none. Of the five families this is the one where the name and the scope are both right; what is missing is the body.

The fix keeps the declaration and adds the definition below it:

C++
#include <iostream>

int main();

int helper() {
    std::cout << "ready\n";
    return 0;
}

int main() {
    return helper();
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fix_main_declared_only fix_main_declared_only.cpp
$ ./fix_main_declared_only
ready

The output is ready, printed by the helper the definition calls. In a multi-file build this family usually means the definition lives in a file that is not part of this link - the declaration was visible, so everything compiled, and the one file that would have satisfied the linker was never handed to it. The repair is the same either way: a definition of main among the files being linked, whether written in place, as here, or restored to the build command.

The fifth failure: a static member function named main

A class whose static member function is named main:

C++
#include <iostream>

struct Program {
    static int main() {
        std::cout << "ready\n";
        return 0;
    }
};
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fail_static_member_main fail_static_member_main.cpp
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

The function here is Program::main - a member of the class Program, not a function in the global namespace. That it is declared static makes it callable without an object, but a static member function still belongs to its class, exactly as the second family's function belonged to its namespace. The global symbol main is undefined, and the linker reports the same three lines. A free-standing static int main() is a different story - the compiler rejects that one itself, as the last trap shows - because main at global scope may not be declared static at all.

The fix renames the member to describe what it does, and adds the global main that calls it:

C++
#include <iostream>

struct Program {
    static int run() {
        std::cout << "ready\n";
        return 0;
    }
};

int main() {
    return Program::run();
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o fix_static_member_main fix_static_member_main.cpp
$ ./fix_static_member_main
ready

The output is ready. The member function keeps the program's logic, under a name that makes no promise it cannot keep, and the global main - the only function the startup code will call - delegates to it. This is the general repair for both scoped families in this guide: logic may live wherever it belongs, in a namespace or a class, as long as a global int main() exists to be found and hands control to it.

The fixes in reach order

  • No definition anywhere: write one - a global int main() that calls the helpers the program already has - or, in a larger build, check that the file defining main is in the build command.
  • A definition inside a namespace: move it to the global namespace, or rename the namespaced function and add a global main that calls it.
  • A definition spelled Main: fix the spelling to main - the symbol lookup is exact and case-sensitive.
  • A declaration with no definition: add the definition, in this file or in a file that is part of the same link; a declaration alone provides no symbol.
  • A member function named main: rename the member, and add a global main that calls it.
  • In every case: the repair is a definition - not a declaration, not a call, not a using-declaration - of a function named main, returning int, in the global namespace, among the files handed to the linker.

Traps

  • A main with no return statement compiles, runs, and exits with status 0. Reaching the end of main without a return statement has the effect of executing return 0; - a privilege of main alone, not of int functions generally:
C++
#include <iostream>

int main() {
    std::cout << "ready\n";
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o probe_no_return probe_no_return.cpp
$ ./probe_no_return; echo "exit: $?"
ready
exit: 0
Trap
The output is ready, followed by the echoed exit status, exit: 0. Do not generalize this shape: an ordinary function that returns int and reaches its end without returning a value is not covered by the rule, and copying the no-return style out of main into a helper is a defect the compiler may not report as an error. Inside main, though, a missing return is never the cause of the undefined-reference error - if that error is what you have, the definition itself is what is missing, not its return statement. - The two-parameter form of main is the other standard shape, and it compiles and receives the arguments the environment passed:
C++
#include <iostream>

int main(int argc, char* argv[]) {
    std::cout << argc << ' ' << argv[0] << '\n';
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o probe_argc probe_argc.cpp
$ ./probe_argc
1 ./probe_argc

The output is 1 ./probe_argc: with no arguments given on the command line, argc is 1 and argv[0] is the name the program was invoked by. The parameter names are arbitrary, and char** may stand in for char* argv[]. A build that fails with undefined reference to main is never repaired by changing between the one-line forms of the signature - both forms define the same symbol - so if a program in one of these forms still fails to link, the definition is missing, misspelled, or scoped, not mis-parameterized. - A main defined in a second file satisfies the linker when both files are linked together. Here the helper and main live in separate files, shown in one listing with each file's name above its contents:

C++
// helper.cpp
#include <iostream>

int helper() {
    std::cout << "ready\n";
    return 0;
}

// main.cpp
int helper();

int main() {
    return helper();
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o probe_two_tu helper.cpp main.cpp
$ ./probe_two_tu
ready

The output is ready, and the compile line is the point: both helper.cpp and main.cpp are named in the one command, so the definition of main from main.cpp is among the objects the linker collects. The definition does not have to live in any particular file, and it does not have to share a file with the code it calls - it has to be in the link. The fourth family is this shape with main.cpp left out: everything still compiles, because declarations are enough for compiling, and the link fails because definitions are what linking needs. - void main() is not this error. The compiler rejects it itself, before any linking happens:

C++
#include <iostream>

void main() {
    std::cout << "ready\n";
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o probe_void_main probe_void_main.cpp
probe_void_main.cpp:3:1: error: ‘::main’ must return ‘int’
    3 | void main() {
      | ^~~~

The diagnostic is "error: '::main' must return 'int'", with the file, line, and the offending declaration pointed at - everything the linker diagnostic withholds. main must return int; that is checked per file, at compile time. Recognizing the difference matters for triage: a transcript with your file name and a caret is a signature problem to fix in the declaration, while the three linker lines with no file of yours named are a missing-definition problem to fix by supplying or unscoping the definition. - A free-standing static int main() is not this error either, and it fails for the mirror-image reason:

C++
#include <iostream>

static int main() {
    std::cout << "ready\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -g -Wall -Wextra -o probe_free_static_main probe_free_static_main.cpp
probe_free_static_main.cpp:3:12: error: cannot declare ‘::main’ to be static
    3 | static int main() {
      |            ^~~~

The diagnostic is "error: cannot declare '::main' to be static", again at compile time, again naming the file and line. main cannot be declared with the static specifier. Compare the fifth family: there, static appeared on a member function, the compiler accepted it - a static member is an ordinary, legal thing - and the failure waited for the linker, because the program had no global main. The same keyword is a compile error at global scope and a link error inside a class, and which transcript you get tells you which of the two you wrote.

Checklist

  • The diagnostic read as a linker report, not a compiler one: Scrt1.o and _start name the startup code making the reference, and collect2 reporting ld exited with status 1 is the summary, not a second error.
  • A definition of main - a body, not a declaration - present among the files handed to the linker in this build.
  • That definition in the global namespace: not inside a namespace, not a member of a class.
  • The name spelled exactly main, lower case.
  • The return type int, in one of the allowed forms: no parameters, or int and char* argv[] (parameter names arbitrary), with noexcept permitted on either.
  • No static specifier on the global main.
  • Logic that lives in a namespace or a class kept there under its own name, with the global main calling it - rather than a scoped function named main expected to be found on its own.
  • The fixed program run, and its output and exit status checked against what the program was meant to do.

Sources

  • cppreference, Main function - for a program containing a global function named main as its designated start, for the allowed forms int main() and int main(int, char**) with arbitrary parameter names and the noexcept forms allowed since C++17, for main not being overloadable and not being declarable as static, for the name main being usable for entities in a non-global namespace, and for reaching the end of main without a return statement having the effect of return 0.
  • GNU, ld: Setting the Entry Point - for the entry point being the first instruction executed in a program and set by the linker, the context for the startup object's reference to main appearing at link time rather than in any one file's compilation.

Keep Exploring