C++
The C++ build process and the "undefined reference" error
The code compiles but the program won't build: "undefined reference to ...". To understand this error, you need to know that building is really three separate stages. Here are the most common causes and fixes.
While learning C++, you will eventually meet this message: undefined reference to 'Account::balance() const'. There's no typo, the compiler doesn't complain, but the program just won't build. The first time I saw it I blamed the compiler; later I realized the error wasn't coming from the compiler at all, but from the linker.
Building isn't one step
When you run g++ main.cpp account.cpp -o program, three separate jobs happen behind the scenes:
- Preprocessing:
#includelines are copied into the file and macros are expanded. - Compilation: each
.cppfile is turned into an object file (.o) separately, unaware of the others. - Linking: object files are combined, and every function call is connected to that function's actual definition.
A header tells the compiler "this function exists"; that's a declaration. The function's body is its definition, and it lives in a .cpp file. The compiler is happy when it sees a declaration. If the linker can't find the definition, it says "undefined reference".
g++ -c main.cpp # main.o: compiled, the balance() call is marked "to be linked later"
g++ -c account.cpp # account.o: balance()'s definition lives here
g++ main.o account.o -o program # the linker joins themThe most common causes
1. Not adding the source file to the build
By far the most common. Writing g++ main.cpp -o program and forgetting account.cpp. In an IDE, the file not being added to the project or to CMakeLists.txt.
add_executable(program main.cpp account.cpp) # account.cpp must be here2. Declaration and definition signatures differ
Writing int balance() const; in the header and forgetting const in the .cpp, or leaving out the class name:
int balance() const { ... } // wrong: you defined a free function
int Account::balance() const { ... } // right3. Putting templates in a .cpp file
Template functions are generated where they're used. If the definition is hidden in a .cpp, other files can't see it and the linker can't find it. Template definitions usually belong in the header.
4. Not defining a static member
Writing static int count; inside a class is only a declaration. Before C++17 you had to define it in a .cpp file as int Counter::count = 0;; since C++17 you can write inline static int count = 0;.
5. Mixing C and C++ code
The C++ compiler "decorates" function names to support overloading (name mangling). When calling a function from a library compiled as C, those names don't match. The fix is to wrap the declaration in extern "C":
extern "C" {
#include "old_c_library.h"
}6. Not linking the library
If you use an external library, including its header isn't enough; you also have to give the library to the linker: -lsqlite3, -lpthread and so on. Order can matter too: libraries should come after the files that use them.
Learning to read the error
The undefined reference message tells you exactly which symbol is missing. If you see a mangled name, c++filt makes it readable. nm -C account.o shows which symbols an object file defines; if the function you're looking for isn't there, the problem isn't the build, it's the definition.
Understanding the build stages taught me not only how to fix this error, but also, in reverse engineering, how a program is put together. The compiler and the linker are a program's invisible architects.