C++
C++ Undefined Reference to Function Fix
The compiler accepted every file, because a declaration is all a caller needs to compile. The linker is stricter: it needs a definition of the exact function the caller named - same scope, same parameter types, same language linkage, with external linkage, among the files in the link. This guide separates the five ways a definition can be present in the project and still not be that definition, reads what the linker diagnostic actually names, and matches each shape to its repair.
Written November 24, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: a call references one exact symbol, and the link must define it
- How to read the diagnostic
- The first failure: the defining file is not in the link
- The second failure: the definition has a different signature
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
A C++ program can pass every compile step and still fail to become a program, because compiling and linking ask different questions. Compiling a file asks whether every name it uses has been declared; a declaration is a promise, and the compiler works on promises. Linking asks whether every promise was kept: for each function a compiled file calls, some file in the link must contain a definition of that exact function. When none does, the linker stops with "undefined reference to" the function - and it does not matter which of several ways the definition went missing.
The five forms in this guide each reproduce the failure with a small, complete program, and the definition goes missing five different ways: the file that defines the function is left out of the link command, the definition's signature differs from the declaration the caller compiled against, the declaration and the definition carry different language linkage, the definition is marked static so it belongs to its own file only, and the definition sits inside a namespace while the caller declares the function at global scope. In each family the failing program is small, the full linker output is shown, and the fix supplies the one definition the linker is looking for while keeping the rest of the program as it was. Five neighboring shapes in the traps are not this error: linking both files works, a definition in the same file as the caller works, a static definition in a header works for the file that includes it, a declaration that is never called needs no definition at all, and a compile-only build never reaches the linker and so never reports the error.
Three neighboring failures have their own guides and are not covered here. A missing main is a different symbol - the reference comes from the startup code, not from a call in your code - and has its own guide. An undefined reference to a vtable names a class, not a function, and has its own guide. And a template whose definition sits in a .cpp file fails for a different reason again - the calling file cannot generate the code - and has its own guide. This guide is about the plain case those three are not: an ordinary, non-template, non-virtual function that your own code calls by name.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a call references one exact symbol, and the link must define it
A function call does not refer to a name in the loose sense. By the time a file is compiled, the call refers to one specific function, and the compiled file records a reference to the symbol for that function. The symbol encodes more than the spelling: for a C++ function it encodes the scope the function lives in and its parameter types, because C++ allows overloading - one name referring to several functions, distinguished by their parameters. Two functions that share a name but differ in parameter types are two different functions, with two different symbols, and a definition of one says nothing about the other.
Language linkage is part of the identity too. Every function name with external linkage has a language linkage - "C++" by default, or "C" when declared inside an extern "C" specification - and language linkage covers the name mangling applied to the symbol. A function declared extern "C" is recorded under its plain name; the same function defined without the specification is recorded under a mangled C++ name. To the linker those are two unrelated symbols, exactly as if the names were spelled differently.
The third part of the identity is linkage in the visibility sense. A name with external linkage can be redeclared in another translation unit and refers to the same entity there - that is what makes a cross-file call possible at all. A name with internal linkage refers to an entity of its own translation unit only: functions declared static at namespace scope have internal linkage, and so does everything declared in an unnamed namespace. A definition with internal linkage is a real definition, but it is a definition of that file's private function, and it cannot satisfy a reference from another file.
So the linker's requirement is exact in four dimensions at once: a definition, of a function with the same scope, the same parameter types, and the same language linkage as the declaration the caller compiled against, with external linkage, in one of the files handed to the linker. Each family below breaks exactly one of those dimensions, and each fix repairs exactly that one.
How to read the diagnostic
Every failure in the five families prints the same shape of output. The first family's, in full:
/usr/bin/ld: /tmp/cctDPNu2.o: in function `main':
main.cpp:(.text+0x13): undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
Read it in order. The first line names the object that contains the unsatisfied reference and the function it sits in: here, the compiled form of main.cpp, in its function main. Unlike a missing-main failure, the reference comes from your own code, and the linker says where. The second line is the failure itself: at offset 0x13 in that function, the symbol for add(int, int) 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 undefined-reference error in each transcript, however the definition went missing: each of these programs references its missing function from one place in one file, so the missing symbol is reported once. A larger program that references the same missing function from other places can print one line per referencing place - one for each call site and each calling file. Second, the symbol as printed is the first piece of evidence. For a C++ function the linker prints the demangled form - add(int, int), compute() - so the parameter types it is looking for are written out; compare them character by character with the definition you believe you have. Third, when the printed name has no parameter list at all - the third family's `greet' - the reference is to a "C"-linkage symbol, and that absence is itself the clue: the caller was compiled against an extern "C" declaration, and whatever definition exists was recorded under a mangled name the plain name will never match.
The first failure: the defining file is not in the link
A caller in main.cpp, a definition in add.cpp, and a link command that names only the caller:
// main.cpp
#include <iostream>
int add(int, int);
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
// add.cpp
int add(int a, int b) {
return a + b;
}
$ g++ -std=c++20 -Wall -Wextra -o fail_missing_file main.cpp
/usr/bin/ld: /tmp/cctDPNu2.o: in function `main':
main.cpp:(.text+0x13): undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
Both files are correct, and add.cpp compiles cleanly on its own. But the command above never compiles or links it: the only object the linker receives is the one built from main.cpp, and that object contains a reference to add(int, int) and no definition. A definition in a file outside the link satisfies nothing, in exactly the way a correct answer in a drawer satisfies no question asked elsewhere. This is the most common form of the error in real builds: a new .cpp file written, saved, and never added to the build - or a build command, a source list, or a glob that quietly does not cover it.
The fix is the command, not the code - the same two files, both named:
// main.cpp
#include <iostream>
int add(int, int);
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
// add.cpp
int add(int a, int b) {
return a + b;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_file main.cpp add.cpp
$ ./fix_missing_file
5
The output is 5, the sum the caller asked add for. Nothing in either file changed; the definition was there all along, and putting its file in the link is the whole repair. When this error appears in a larger build, the equivalent check is the list of files the build actually hands to the compiler - not the list of files in the directory.
The second failure: the definition has a different signature
A caller compiled against int add(int, int), a definition of int add(int, int, int), and both files in the link:
// main.cpp
#include <iostream>
int add(int, int);
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
// add.cpp
int add(int a, int b, int c) {
return a + b + c;
}
$ g++ -std=c++20 -Wall -Wextra -o fail_signature_mismatch main.cpp add.cpp
/usr/bin/ld: /tmp/ccr7hedv.o: in function `main':
main.cpp:(.text+0x13): undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
Here the defining file is in the link, and it defines a function named add - the wrong one. Functions that differ in their parameter lists are different functions: add(int, int, int) is an overload, a separate entity with its own symbol, and defining it leaves add(int, int) exactly as undefined as if no add existed at all. Both files compile, because the caller's file sees a declaration it can call and the defining file defines a legal function; neither compile step compares the two parameter lists against each other. The mismatch is typically a signature edited in one place - a parameter added to the definition, a double where the declaration says int, a const dropped from a pointer parameter's pointee - while the declaration the callers include kept the old form.
The fix makes the definition match the declaration the caller used:
// main.cpp
#include <iostream>
int add(int, int);
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
// add.cpp
int add(int a, int b) {
return a + b;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_signature_mismatch main.cpp add.cpp
$ ./fix_signature_mismatch
5
The output is 5. Read the diagnostic literally when this family is yours: the linker prints the full signature it needs, add(int, int), and the repair is a definition with exactly those parameter types - not a function with the same name and a nearby signature. If the longer signature is the one the program now wants, the repair runs the other way: change the declaration the callers see, and their calls, to match the definition. What cannot work is leaving the two lists different and expecting the linker to treat them as one function.
The third failure: the declaration and definition have different language linkage
A header that declares greet as extern "C", a caller that includes it, and a definition written as an ordinary C++ function:
// greet.h
#pragma once
extern "C" int greet();
// main.cpp
#include "greet.h"
int main() {
return greet();
}
// greet.cpp
#include <iostream>
int greet() {
std::cout << "ready\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fail_linkage_mismatch main.cpp greet.cpp
/usr/bin/ld: /tmp/ccTHjNfb.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `greet'
collect2: error: ld returned 1 exit status
The diagnostic gives this family away: the missing symbol is printed as plain `greet', with no parameter list, because a "C"-linkage name is recorded unmangled. The caller's file, compiled against the header, references that plain symbol. The definition in greet.cpp is a C++ function - that file never includes the header, so nothing told it otherwise - and it is recorded under the mangled C++ symbol for greet(). Both files compile; the link fails because the plain name and the mangled name are different symbols, and only the mangled one is defined. This shape appears wherever a header is shared with C code, or an extern "C" block was added around declarations in a header without the defining file being brought under the same declaration.
The fix brings the definition under the same declaration - the defining file includes the header, and a definition that follows an extern "C" declaration takes the "C" linkage from it:
// greet.h
#pragma once
extern "C" int greet();
// main.cpp
#include "greet.h"
int main() {
return greet();
}
// greet.cpp
#include <iostream>
#include "greet.h"
int greet() {
std::cout << "ready\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_linkage_mismatch main.cpp greet.cpp
$ ./fix_linkage_mismatch
ready
The output is ready. Including the header in the defining file is the durable form of this fix: the declaration and the definition then come from one text, and a later edit to the header reaches both. Writing extern "C" on the definition by hand repairs the same link, but leaves two copies of the fact to drift apart. The mirror-image mismatch - a plain C++ declaration in the header and an extern "C" definition - fails the same way with the roles of the plain and mangled symbols exchanged, and is repaired the same way.
The fourth failure: the definition is marked static
A caller that declares compute in the ordinary way, and a definition marked static in its own file:
// main.cpp
int compute();
int main() {
return compute();
}
// compute.cpp
#include <iostream>
static int compute() {
std::cout << "ready\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fail_static_definition main.cpp compute.cpp
compute.cpp:3:12: warning: ‘int compute()’ defined but not used [-Wunused-function]
3 | static int compute() {
| ^~~~~~~
/usr/bin/ld: /tmp/ccdsspFq.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `compute()'
collect2: error: ld returned 1 exit status
The compiler leaves a clue before the linker speaks: the warning that compute is "defined but not used" in compute.cpp. Within that file the warning is accurate - nothing there calls it - and it is the visible sign of what static did. A function declared static at namespace scope has internal linkage: the definition belongs to its own translation unit, as a private function of that file, and no reference from another file can reach it. The caller's file compiles against its own declaration, which promises an external function; the defining file provides a private one instead; the link fails on the external symbol, compute(), that nobody defined.
The fix removes the one word:
// main.cpp
int compute();
int main() {
return compute();
}
// compute.cpp
#include <iostream>
int compute() {
std::cout << "ready\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_static_definition main.cpp compute.cpp
$ ./fix_static_definition
ready
The output is ready, and the warning is gone with the error. static on a namespace-scope function is the right tool when a helper is meant for its own file only - that is what internal linkage is for - and this family is what it looks like when a function outgrows that intent: the moment another file needs to call it, the static has to go, or the calls have to move into the defining file. Note the contrast with the third trap: a static function defined in a header works for callers that include the header, because each including file gets its own private copy and calls its own - no cross-file reference ever forms.
The fifth failure: the definition is inside a namespace
A caller that declares compute at global scope, and a definition inside the namespace util:
// main.cpp
int compute();
int main() {
return compute();
}
// compute.cpp
#include <iostream>
namespace util {
int compute() {
std::cout << "ready\n";
return 0;
}
} // namespace util
$ g++ -std=c++20 -Wall -Wextra -o fail_namespace_definition main.cpp compute.cpp
/usr/bin/ld: /tmp/ccUyCXm4.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `compute()'
collect2: error: ld returned 1 exit status
This program does define a function named compute - util::compute. That is a different function from ::compute. A name declared inside a namespace belongs to that namespace, so the definition's symbol carries the namespace with it, and the global symbol the caller's declaration promised is never defined. Both files compile: the caller's declaration is a legal promise, and the namespaced definition is a legal function that simply is not the promised one. The unnamed-namespace variant of this family is stricter still - names in an unnamed namespace have internal linkage, so that definition is private to its file as well as differently named.
The fix declares and calls the function that actually exists:
// main.cpp
namespace util {
int compute();
} // namespace util
int main() {
return util::compute();
}
// compute.cpp
#include <iostream>
namespace util {
int compute() {
std::cout << "ready\n";
return 0;
}
} // namespace util
$ g++ -std=c++20 -Wall -Wextra -o fix_namespace_definition main.cpp compute.cpp
$ ./fix_namespace_definition
ready
The output is ready, printed by the same util::compute that was there all along; the definition did not move. The repair has two directions and the choice is about which name the program means: if the function belongs in the namespace, declare it there in the caller's file - or, in real code, include the header that declares it there - and call it qualified; if the function is meant to be global, move the definition out of the namespace. What does not work is a using-declaration in the caller's file offered as a substitute for a declaration of the right function: the call still has to name a declared function whose scope matches a definition, and the mismatch is in the declaration, not in how the call is spelled.
The fixes in reach order
- The defining file is not in the link: add it to the build command or source list - the definition exists and only needs to be handed to the linker.
- The definition's signature differs from the declaration: make the parameter lists identical, in whichever of the declaration or the definition is the stale one; the linker prints the exact signature it needs.
- The language linkage differs: include the header in the defining file, so the definition takes the declaration's linkage - plain name and mangled name are different symbols.
- The definition is
static: removestaticwhen another file must call the function; internal linkage makes the definition private to its file. - The definition is in a namespace: declare and call the function in that namespace, or move the definition to the scope the caller declares.
- In every case: the repair is a definition with external linkage, in the link, whose scope, parameter types, and language linkage are exactly the ones the caller's declaration named.
Traps
- Both files in the link command is the whole of the first family's repair, and it is worth seeing on a program that was never broken. Here a helper and
mainlive in separate files, shown in one listing with each file's name above its contents, and the command names both:
// helper.cpp
#include <iostream>
int helper() {
std::cout << "ready\n";
return 0;
}
// main.cpp
int helper();
int main() {
return helper();
}
$ g++ -std=c++20 -Wall -Wextra -o probe_both_files helper.cpp main.cpp
$ ./probe_both_files
ready
ready. A definition does not have to live in any particular file, and it does not have to share a file with the code that calls it - it has to be in the link. The first family is this shape with the defining file left out of the command: everything still compiles, because declarations are enough for compiling, and the link fails because definitions are what linking needs.
- A definition in the same file as the caller satisfies the linker trivially, because no cross-file reference is involved at all:#include <iostream>
int compute() {
std::cout << "ready\n";
return 0;
}
int main() {
return compute();
}
$ g++ -std=c++20 -Wall -Wextra -o probe_same_tu probe_same_tu.cpp
$ ./probe_same_tu
ready
The output is ready. This is the shape small programs start in, and it is why this error tends to arrive exactly when a program grows: the failure appears at the moment a definition and its callers first land in different files, and one of the four dimensions - file list, signature, linkage, scope - fails to survive the move.
- A static function defined in a header works for a caller that includes the header - the opposite outcome from the fourth family, from the same keyword:
// compute.h
#pragma once
#include <iostream>
static int compute() {
std::cout << "ready\n";
return 0;
}
// main.cpp
#include "compute.h"
int main() {
return compute();
}
$ g++ -std=c++20 -Wall -Wextra -o probe_static_header main.cpp
$ ./probe_static_header
ready
The output is ready. main.cpp includes compute.h, so the definition is compiled into main.cpp's own translation unit, and the call is satisfied by that unit's private copy; no cross-file reference forms, so internal linkage has nothing to block. The price of the shape is that every file including the header gets its own copy of the function - correct, and sometimes exactly what a small helper wants, but a different arrangement from one shared definition, and the reason static in a .cpp file and static in a header behave so differently.
- A declaration with no call behind it needs no definition. This program declares never_defined, defines nothing, and links:
#include <iostream>
int never_defined(int);
int main() {
std::cout << "ready\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_dead_declaration probe_dead_declaration.cpp
$ ./probe_dead_declaration
ready
The output is ready. A declaration is a promise the compiler accepts and the linker never audits on its own: a reference is created by a call (or by taking the function's address), and only a reference sends the linker looking for a definition. This is why the error always names a function the program actually uses - and why a declared-but-undefined function can sit in a header unnoticed until the first call turns it into a linker error.
- Compiling without linking never reports this error, because the error is the linker's. The first family's caller, compiled with -c - compile only, producing an object file and no program:
#include <iostream>
int add(int, int);
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o probe_compile_only.o probe_compile_only.cpp
exit: 0
The command exits with status 0 and produces probe_compile_only.o, an object file that still contains the unsatisfied reference to add(int, int) - nothing has been repaired, and the same file handed to a link fails as the first family shows. A build in which every step "compiles fine" and the failure appears only at the final link is this trap at project scale: the compile steps were never capable of reporting the problem, because checking references against definitions is not their step.
Checklist
- The diagnostic read as a linker report: the first line names the object and function holding the reference, the second names the exact symbol missing, and
collect2reportingldexited with status 1 is the summary, not a second error. - The printed symbol compared with the definition in hand: same scope, same parameter types - and if the printed name has no parameter list, the reference is a
"C"-linkage one and the definition's language linkage is the mismatch. - A definition - a body, not a declaration - of that exact function among the files handed to the linker in this build.
- That definition with external linkage: not
static, not in an unnamed namespace. - The defining file present in the build command or source list, not merely present in the directory.
- The defining file including the header that declares the function, so signature and language linkage come from one text.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Fix "Undefined Reference to Vtable" — the Missing Virtual Function
- Undefined Reference to Typeinfo Fix
- Undefined Reference to Static Member Fix
- C++ Undefined Reference to Main Fix
Sources
- cppreference, Language linkage - for every function name with external linkage having a language linkage,
"C++"by default and"C"under anextern "C"specification, for language linkage covering the name-mangling algorithm, and for a redeclaration without a linkage specification inheriting the entity's existing language linkage. - cppreference, Storage class specifiers - for a name with external linkage being redeclarable in another translation unit, for functions declared
staticat namespace scope having internal linkage, and for all names declared in an unnamed namespace having internal linkage. - cppreference, Overload resolution - for a function name referring to more than one entity being overloaded, with the compiler determining which overload a call refers to - the basis for a definition with different parameter types being a different function from the one called.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.