
-lm for math), a library named before the files that use it, or C++ code built with gcc where it needs g++.This error confuses people because the code looks right. It is right, as far as the compiler can tell. The problem is in how the program is being put together.
We reproduced every message below with gcc 13 on Linux before writing it down.
What the linker does
Building a C or C++ program takes two steps.
- CompileEach source file is turned into an object file on its own. The compiler only needs a declaration of each function you call. It trusts that the body exists somewhere.
- LinkThe linker joins the object files and libraries into one program. Now every function that was called must actually be found.
The Linux manual describes the linker, ld, like this: it “combines a number of object and archive files, relocates their data and ties up symbol references”. It adds: “Usually the last step in compiling a program is to run ld.”
“Undefined reference” means one of those symbol references could not be tied up.
You can see this for yourself with the nm tool, which lists the names in an object file. Our main.c calls a function named total that lives in stats.c:
U total
0000000000000000 T totalIn main.o the name is marked U, for undefined: used here, defined elsewhere. In stats.o it is marked T: the body is here. The linker’s job is to match the two. When no file supplies the T, you get the error.
How to read the message
/usr/bin/ld: /tmp/ccXXXXXX.o: in function `main':
main.c:(.text+0x3d): undefined reference to `total'
collect2: error: ld returned 1 exit status- The first line says where the name was used: in
main, inmain.c. The/tmp/cc...opart is a temporary file, and its name changes on every build. - The second line gives the name that could not be found:
total. - The last line,
ld returned 1 exit status, confirms that the linker failed, not the compiler.
One clue is worth learning. If the name is plain, like total, the linker wanted a C function. If it comes with parameter types, like total(int const*, int), it wanted a C++ function.
The 8 causes
| Cause | The missing name | The fix |
|---|---|---|
| 1. A source file left out of the build | total | Name every source file |
| 2. Declared, but never written or misspelled | total | Write it, with the exact name |
| 3. A library that was not linked | sqrt | Add -lm |
| 4. A library named before the files that use it | sqrt | Put libraries last |
| 5. C++ built with gcc | std::cout | Use g++ |
| 6. A class member with no body | vtable for Shape | Define it |
| 7. A template defined in a .cpp file | int biggest<int>(int, int) | Move it to the header |
| 8. A C function called from C++ | total(int const*, int) | Wrap the header in extern "C" |
1. A source file left out of the build
The most common cause. The function exists, in a file the linker was never given.
gcc main.c -o appgcc main.c stats.c -o appIncluding the header is not enough. #include "stats.h" gives the compiler the declaration. It does not bring in the code. If you use a Makefile, check that the new file’s object is in the link rule.
2. Declared, but never written or misspelled
The header promises a function called total. The source file defines totl. The compiler is happy with both files. The linker cannot match them, and the message is identical to cause 1.
int totl(const int *values, int count) {int total(const int *values, int count) {In C++ this happens with a method declared in the class and never given a body. We declared size() in a Stack class and forgot to write it:
undefined reference to `Stack::size() const'Search your source files for the name. If it appears only in a header, you have found the problem.
3. A library that was not linked
#include <math.h> declares sqrt and pow. Their code lives in a separate library. The manual page for sqrt lists it under “Math library (libm, -lm)”.
gcc calc.c -o calcgcc calc.c -o calc -lmThreads are a similar case. The Linux manual says “programs that use the Pthreads API should be compiled using cc -pthread”. On our machine a small thread program linked without the flag. Other systems may refuse, and so may your course’s grader, so add -pthread whenever you use threads.
4. A library named before the files that use it
This one feels unfair. The library is in the command, and the error is the same as if it weren’t.
gcc -lm calc.c -o calcgcc calc.c -o calc -lmThe gcc manual explains it: “It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified.” Its example is foo.o -lz bar.o, and it warns: “If bar.o refers to functions in z, those functions may not be loaded.”
The linker manual says the same about archives: “The linker will search an archive only once, at the location where it is specified on the command line.” We built our own small library to check. gcc -L. -lstats main.c failed, and gcc main.c -L. -lstats worked.
The rule to remember: source and object files first, libraries last.
5. C++ built with gcc
gcc hello.cpp -o hellog++ hello.cpp -o hellogcc will compile a .cpp file. It will not link the C++ standard library for you. The manual says so plainly: “the use of gcc does not add the C++ library”, while g++ “automatically specifies linking against the C++ library”.
6. A class member with no body
Three C++ mistakes belong here. Each one compiled and then failed at the link step for us.
A virtual function that is declared and never defined. The message does not name your function. It names something called a vtable:
undefined reference to `vtable for Shape'Either write the function, or make it pure virtual by ending its declaration with = 0. The second fixed ours.
A static data member. Declaring static int count; inside a class does not create it.
class Counter {
public:
static int count;
};class Counter {
public:
static int count;
};
int Counter::count = 0;A method that was never written. That is the Stack::size() example from cause 2.
7. A template defined in a .cpp file
We put a template’s declaration in biggest.h and its body in biggest.cpp, the way you would for an ordinary function. It failed:
undefined reference to `int biggest<int>(int, int)'Moving the body into the header fixed it. A template is a pattern, and the compiler has to see the whole pattern in every file that uses it.
8. A C function called from C++
We called our C function total from a C++ file. Both files compiled. The link failed:
undefined reference to `total(int const*, int)'This is the clue from earlier: the name has parameter types, so the linker was searching for a C++ function. The function was compiled as C and is stored under the plain name total.
extern "C" {
#include "stats.h"
}Two messages about main
undefined reference to `main'Every program needs exactly one main. You get this when the file that holds main was left out, or when main is misspelled.
The opposite problem has its own message:
multiple definition of `main'This matters for homework. Many autograders supply their own main and link it with your file. If you leave your own main in, the grader’s build fails. If the grader’s tests call a function you never wrote, you get an undefined reference in the grader’s file, not yours. Our guide to why code works locally but fails the autograder has more on both.
Find which cause is yours
- Whose name is it?If you wrote the function, go to step 2. If it belongs to a library, such as
sqrtorstd::cout, go to step 4. - Does a body exist?Search your source files for the name. A declaration in a header is not a body. Check the spelling and the parameter types.
- Is that file in the build?Read the build command or the Makefile. Every source file must be named.
- Is the library linked, and last?Add its
-lflag after your files. For C++, build withg++.
Still stuck?
Send us your files and the full build output. A senior engineer finds the cause, fixes it and explains why it broke, so you can explain it too. On our debug my code page, a single bug or error message is typically $45–$70, usually the same day.
Related: C assignment help, C++ homework help and operating systems homework help. If your program builds and then crashes, see our guide to the segmentation fault. For background on the tools, see our post on C++ compilers.
Questions about this error
What does undefined reference mean in C?
It means the linker could not find the body of a function or variable that your code uses. The code compiled, because the compiler only needs a declaration. The error appears at the last step, when the pieces are joined into one program and one piece is missing.
Is undefined reference a compiler error or a linker error?
It is a linker error. You can tell from the last line of the output, which says that ld returned 1 exit status. The compiler finished without complaint, and the linker failed afterwards.
Why do I get undefined reference to sqrt when I included math.h?
The header only declares the function. Its code is in the math library, which is not linked unless you ask. Add the -lm option at the end of your build command, after your source files.
Why does the order of files and libraries matter?
The linker works through the command from left to right and looks in each library only once, at the point where it is named. A library named before the file that needs it has already been passed. Put your source and object files first and libraries last.
What does undefined reference to main mean?
The linker found no main function in the files it was given. Either the file that contains main was left out of the build command, or main is misspelled. Every program needs exactly one.
What does undefined reference to vtable mean in C++?
It usually means a virtual function was declared in a class and never given a body. Write the function, or make it pure virtual if derived classes are meant to supply it.
Sources
- Linux manual: gcc(1)
- Linux manual: ld(1)
- GNU linker manual: command-line options
- Linux manual: sqrt(3)
- Linux manual: pthreads(7)
The C++ results in causes 6, 7 and 8 are from our own builds with g++ 13.
Upload the prompt and see a predicted budget in under a minute. A senior engineer confirms the final price.


