Do My Programming HomeworkDo My Programming Homework

“undefined reference to” in C and C++: every cause and fix

Your code compiled, and then the linker could not find the body of something you used. Here is how to read the message, the 8 causes with real output, and the fix for each.

DoMyProgrammingHomework.io engineering teamReviewed by Pollen Victory, founderPublished Nov 24, 2026 · 9 min read
A C file that calls a function from another file, and the linker message undefined reference to total
The short answer
“undefined reference to” is a linker error, not a compiler error. Your code compiled. Then the linker tried to join the pieces into one program and could not find the body of a function or variable you used. The name in quotes is the missing piece. The usual causes are a source file left out of the build command, a function that was declared but never written, a library that was not linked (such as -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.

From source files to a program
  1. 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.
  2. 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:

nm main.o, then nm stats.otext
                 U total
0000000000000000 T total

In 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

What the linker printstext
/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, in main.c. The /tmp/cc...o part 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

The 8 causes, with the name the linker could not find in our builds
CauseThe missing nameThe fix
1. A source file left out of the buildtotalName every source file
2. Declared, but never written or misspelledtotalWrite it, with the exact name
3. A library that was not linkedsqrtAdd -lm
4. A library named before the files that use itsqrtPut libraries last
5. C++ built with gccstd::coutUse g++
6. A class member with no bodyvtable for ShapeDefine it
7. A template defined in a .cpp fileint 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.

Fails: stats.c is not in the commandtext
gcc main.c -o app
Passes: every source file is namedtext
gcc main.c stats.c -o app

Including 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.

Fails: the header says total, the definition says totlc
int totl(const int *values, int count) {
Passes: the names match exactlyc
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:

The method exists only in the class declarationtext
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)”.

Fails with undefined reference to `sqrt' and `pow'text
gcc calc.c -o calc
Passes: -lm links the math librarytext
gcc calc.c -o calc -lm

Threads 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.

Fails: -lm comes before the file that needs ittext
gcc -lm calc.c -o calc
Passes: libraries go after the files that use themtext
gcc calc.c -o calc -lm

The 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

Fails with undefined reference to `std::cout'text
gcc hello.cpp -o hello
Passestext
g++ hello.cpp -o hello

gcc 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:

A virtual function without a bodytext
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.

Fails with undefined reference to `Counter::count'cpp
class Counter {
public:
    static int count;
};
Passes: one definition outside the classcpp
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:

The template body was in a separate .cpp filetext
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:

A C function, used from C++text
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.

Passes: tell C++ that these declarations are Ccpp
extern "C" {
#include "stats.h"
}

Two messages about main

No main function anywhere in the buildtext
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:

Two main functions in the buildtext
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

Four questions, in order
  1. Whose name is it?If you wrote the function, go to step 2. If it belongs to a library, such as sqrt or std::cout, go to step 4.
  2. 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.
  3. Is that file in the build?Read the build command or the Makefile. Every source file must be named.
  4. Is the library linked, and last?Add its -l flag after your files. For C++, build with g++.

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.

Free price checkFind out what a fix would costUpload your code and the error message. You see a price range in under a minute, with no email needed. Your fixed price never goes above the top of that range.
Check your course rules first
Rules on outside help differ between schools, courses and assignments. Read your syllabus and honor code before you use any help, ours included. Our academic integrity page says what we do and what we won’t.

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

The C++ results in causes 6, 7 and 8 are from our own builds with g++ 13.

Stuck on your own assignment?

Upload the prompt and see a predicted budget in under a minute. A senior engineer confirms the final price.

Keep reading

Related guides