Open In App

How Can I Solve the Error LNK2019 Unresolved External Symbol?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Errors are typical while working with C++ programs, and LNK2019: Unresolved External Symbol – Function is one particularly such annoying issue that occurs when the declared and used function or variable in our code is not found by the linker. In this article, we will learn how to solve the Error LNK2019 in C++.

Solving the Error LNK2019: Unresolved External Symbol – Function

The error LNK2019 is a linker error that occurs when the linker cannot find the definition for a declared function that can happen due to several reasons:

  • When we’ve declared a function but haven’t defined it. Ensure that all declared functions are also defined.
  • If we’re using multiple project files, ensure that they’re correctly set up to link together.
  • The function’s declaration and definition signatures must match. If they don’t, the linker might not be able to find the correct function.
  • If the function is part of a static library that isn’t correctly linked, we may encounter this error so make sure to correctly link all necessary libraries.

To resolve the LNK2019 error, we can follow the below approaches:

1. Check Function Declarations and Definitions

Ensure that all function declarations and definitions match exactly. Pay close attention to function names, return types, and parameter lists.

Example:

The below example demonstrates a simple example of a situation that would cause this error.

C++
#include <iostream>
using namespace std;

// Function declaration
void myFunction();

int main()
{
    // Function call
    myFunction();
    return 0;
}

// No function definition for myFunction


Output

/usr/bin/ld: /tmp/ccd9AbhD.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `myFunction()'
collect2: error: ld returned 1 exit status

To fix this error, we need to provide a definition for myFunction:

C++
#include <iostream>
using namespace std;

// Function declaration
void myFunction();

int main()
{
    // Function call
    myFunction();
    return 0;
}

// Function definition
void myFunction()
{
    // Function body
    cout << "Hello, World!";
}

Output
Hello, World!

2. Verify Header Inclusion

If the function is declared in a header file and defined in a source file, make sure you include the header file where the function is declared in all source files that use it.

C++
// Example: Header file (myHeader.h)
#ifndef MYHEADER_H
#define MYHEADER_H

void myFunction(int arg);

#endif


Output

/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

3. Examine the Visibility of the Function

Check to see if the function you’re attempting to use is available within the current scope. Make that the function is marked as extern in a header file and correctly linked during compilation if it is defined in another source file.

4. Connect the Necessary Libraries

Make sure to link the relevant library during the compilation’s linking stage if the function is declared in an external library.

5. Construct the Project Again

Rebuild your project after making the required adjustments to make sure all files are linked and compiled to the updated to the latest version.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads