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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay