Build fails: undefined reference — how to find and fix the error

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Undefined reference" — the main enemy of a C++ beginner (and not only)

\n\n

You write code, everything looks perfect, the compiler is silent, you press "Build" — and then… a wall of red text. The main character of this nightmare is undefined reference (or unresolved external symbol). Sound familiar? If you've ever written in C++, you've likely encountered this error. It scares beginners, but in reality it's just a signal: "I know which function you're talking about, but I can't find its implementation."

\n\n

In this tutorial, we'll break down what the undefined reference error is, why it occurs, and most importantly — how to fix it. You'll learn the 5 most common causes and get a ready-made checklist for troubleshooting. Let's go!

\n\n

What is undefined reference / unresolved external symbol?

\n\n

Let's start with a little theory. The C++ program build process consists of two stages:

\n
    \n
  • Compilation — each .cpp file is turned into an object file (.o or .obj). The compiler checks syntax and remembers which functions are declared (prototypes).
  • \n
  • Linking — the linker connects all object files and libraries into a single executable file. At this stage, it looks for implementations of all promised functions.
  • \n
\n\n

The undefined reference error (in Visual Studio — unresolved external symbol) occurs precisely at the linking stage. The compiler succeeded, but the linker couldn't find the function body. It screams: "You said the function foo() exists, but I can't find where it is defined!".

\n\n

Top 5 causes of the undefined reference error in C++

\n\n

Let's go through the most common scenarios with real examples. In each case, I'll show the erroneous code and the fix.

\n\n

1. You declared but did not define the function

\n

A classic. You wrote a function prototype but forgot the implementation.

\n
// main.cpp\n#include <iostream>\n\nvoid hello(); // declaration (prototype)\n\nint main() {\n    hello();\n    return 0;\n}\n// Implementation of hello() is missing!\n
\n

Linker error: undefined reference to `hello()'. Fix: write the function body.

\n\n

2. You did not include the file with the implementation in the build

\n

This is a common problem when working with multiple .cpp files. Suppose you declared a function in utils.h, defined it in utils.cpp, but forgot to add utils.cpp to the compilation command.

\n
// utils.h\nvoid printMessage();\n\n// utils.cpp\n#include <iostream>\n#include "utils.h"\nvoid printMessage() {\n    std::cout << "Hello!" << std::endl;\n}\n\n// main.cpp\n#include "utils.h"\nint main() {\n    printMessage();\n    return 0;\n}\n
\n

If you compile only main.cpp (g++ main.cpp), the linker won't see utils.cpp. Fix: compile both files together: g++ main.cpp utils.cpp -o program.

\n\n

3. Class or template defined in .cpp instead of .h

\n

This is a specific pain point in C++. If you define a class method directly in a .cpp file but only include the header, the linker won't find the implementation.

\n
// MyClass.h\nclass MyClass {\npublic:\n    void doSomething();\n};\n\n// MyClass.cpp\n#include "MyClass.h"\nvoid MyClass::doSomething() { /* ... */ }\n\n// main.cpp\n#include "MyClass.h"\nint main() {\n    MyClass obj;\n    obj.doSomething(); // Error: undefined reference\n    return 0;\n}\n
\n

Fix: make sure MyClass.cpp is included in the build (see point 2).

\n\n

4. Incorrect linking of libraries (e.g., math.h)

\n

Some libraries (like the math library libm) require explicit spec

Blogs

Book Recommendations