What is an undefined reference or unresolved external symbol error and how to fix it

The “undefined reference” or “unresolved external symbol” error occurs during the linking stage of the compilation process. It typically means the linker cannot find the definition of a function, variable, or object that was declared but not implemented or linked correctly.


Causes of the Error

  1. Missing Implementation:
    • A function or variable is declared in a header file or source file but not defined anywhere in the project.
  2. Incorrect Linking of Object or Library Files:
    • The linker can’t find the necessary object files or libraries containing the definitions of functions or variables.
  3. Mismatched Function Signatures:
    • A function is declared with a certain signature but defined with a different one.
  4. Static Libraries Not Linked:
    • A required static library is not explicitly linked during compilation.
  5. C++ Name Mangling:
    • Function names in C++ are mangled (encoded) for overloading, and issues may arise if functions are not declared extern "C" when mixed with C code.

How to Fix the Error

1. Provide the Missing Definition

If a function or variable is declared but not defined, you must implement it in a source file.

Example:
// main.cpp
#include <iostream>

void sayHello(); // Declaration

int main() {
sayHello();
return 0;
}

If sayHello() is declared but not defined, the linker will throw an error. To fix:

// sayHello.cpp
#include <iostream>

void sayHello() { // Definition
std::cout << "Hello, World!" << std::endl;
}

Then compile and link both files:

g++ main.cpp sayHello.cpp -o program

2. Link All Required Object Files or Libraries

If the function or variable is defined in another file or library, ensure it is included during linking.

Example:

If using a library libmath.a for math functions:

g++ main.cpp -o program -L/path/to/library -lmath
  • -L: Specifies the library path.
  • -l: Links the library by name (libmath.a becomes -lmath).

3. Ensure Correct Function Signature

Verify that the declaration and definition of a function match exactly in terms of:

  • Return type
  • Parameter types
  • const qualifiers
Example:
// Declaration
void greet(const std::string& name);

// Definition (Incorrect)
void greet(std::string name) { ... } // Mismatch: Missing 'const &' qualifier

Fix by ensuring both declaration and definition match:

void greet(const std::string& name) { ... }

4. Use extern "C" for C++/C Compatibility

When linking C libraries in C++ code, use extern "C" to prevent name mangling.

Example:
// C++ Code
extern "C" {
#include "c_library.h"
}

5. Include All Required Source Files

If your project consists of multiple .cpp files, ensure all files are included during compilation.

Example:
g++ main.cpp file1.cpp file2.cpp -o program

6. Verify Library Compatibility

If you’re linking to a precompiled library, ensure:

  • The library was compiled with the same compiler and version.
  • The library is built for the correct architecture (e.g., x64 vs x86).

7. Check Compiler/Linker Flags

Ensure necessary flags are included for linking libraries, especially for dynamic libraries.

Example:
g++ main.cpp -o program -ldl  # Link with dynamic linking library

Debugging Tips

  1. Verbose Linker Output: Add -v (GCC) or /VERBOSE (MSVC) to view detailed linker output.
    g++ -v main.cpp -o program
  2. Check Symbol Names: Use tools like nm (on Linux) or dumpbin (on Windows) to check for symbols in object files or libraries:bash
    nm -C libmath.a | grep function_name
  3. Ensure Include Guards or #pragma once: Prevent multiple inclusions of header files by using include guards or #pragma once.

Common Example

Problem:

// main.cpp
#include "math_operations.h"

int main() {
int result = add(2, 3); // Undefined reference to 'add'
return 0;
}

math_operations.h:

int add(int a, int b); // Declaration

Solution: Add the definition in math_operations.cpp:

int add(int a, int b) {
return a + b;
}

Compile and link:

g++ main.cpp math_operations.cpp -o program

No images available.