DEV Community

Cover image for How to fix undefined reference in C++?
pikoTutorial
pikoTutorial

Posted on β€’ Originally published at pikotutorial.com

How to fix undefined reference in C++?

Welcome to the next pikoTutorial !

The error we're handling today is a C++ linker error:

undefined reference to `X`
collect2: error: ld returned 1 exit status
Enter fullscreen mode Exit fullscreen mode

What does it mean?

In C++, an "undefined reference" error occurs during the linking stage. This error indicates that the linker cannot find the definition for a function, variable or object that has been declared but not defined. It typically happens when the code is separated into multiple files, and the linker cannot locate the necessary object files or libraries where the symbol is defined.

How to fix it?

Missing definition

Look at this code:

void function();

int main()
{
    function();
}
Enter fullscreen mode Exit fullscreen mode

The function has been declared, but there's no definition, so the linker will complaining throwing the following error:

main.cpp:(.text+0x9): undefined reference to `function()'
collect2: error: ld returned 1 exit status
Enter fullscreen mode Exit fullscreen mode

To fix this, we need to add the implementation of the function:

void function()
{
    // implementation
}

int main()
{
    function();
}
Enter fullscreen mode Exit fullscreen mode

Now everything works as expected.

Declaration/definition mismatch

Look at these 3 files:

function.h

void function(int);
Enter fullscreen mode Exit fullscreen mode

function.cpp

#include "function.h"

void function()
{
    // implementation
}
Enter fullscreen mode Exit fullscreen mode

main.cpp

#include "function.h"

int main()
{
    function(2);
}
Enter fullscreen mode Exit fullscreen mode

In this case we get an error:

main.cpp:(.text+0xe): undefined reference to `function(int)'
collect2: error: ld returned 1 exit status
Enter fullscreen mode Exit fullscreen mode

Linker tells us that it can't find definition of function(int) and indeed, when we look closer at the function definition in .cpp file we see that although it has the same return type and the same name, it has no input argument, so the linker treats it as a completely different function than the one which has been declared in .h file. After adding the int input argument in the function's definition, everything works fine:

void function(int)
{
    // implementation
}
Enter fullscreen mode Exit fullscreen mode

File not added to the compilation

Look at these 4 files:

function.h

void function(int);
Enter fullscreen mode Exit fullscreen mode

function.cpp

#include "function.h"

void function(int)
{
    // implementation
}
Enter fullscreen mode Exit fullscreen mode

main.cpp

#include "function.h"

int main()
{
    function(2);
}
Enter fullscreen mode Exit fullscreen mode

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(MyProject)
add_executable(MyExec main.cpp)
Enter fullscreen mode Exit fullscreen mode

Here, although the function definition is present and there is no declaration/definition mismatch, we still get the same linker error. The reason is visible in the last line of CMakeLists.txt file. We've included in the compilation only source file (main.cpp) out of 2 that we have (main.cpp and function.cpp). So despite the function definition being present in the source code, it is not included in the compilation process. To fix this, we need to add function.cpp file in the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.15)
project(MyProject)
add_executable(MyExec main.cpp function.cpp)
Enter fullscreen mode Exit fullscreen mode

Library with the function definition not linked

Now let's assume that we have the same set of files as in the previous example, but with one change - this time function() is defined and declared in a separate library:

cmake_minimum_required(VERSION 3.15)
project(MyProject)
add_library(FunctionLib function.cpp)
add_executable(MyExec main.cpp)
Enter fullscreen mode Exit fullscreen mode

Despite the fact that the function definition exists, is correct and even function.cpp file is successfully compiled into a library, the whole process fails because the linker still complains about the undefined reference to function(int). The reason is that although FunctionLib has been successfully compiled, it has not been linked to our executable MyExec. After linking everything works as expected:

cmake_minimum_required(VERSION 3.15)
project(MyProject)
add_library(FunctionLib function.cpp)
add_executable(MyExec main.cpp)
target_link_libraries(MyExec PRIVATE FunctionLib)
Enter fullscreen mode Exit fullscreen mode

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay