How do I fix 'LNK2019: unresolved external symbol' on MSVC in C++?

The 'LNK2019: unresolved external symbol' error in MSVC occurs when the linker cannot find a definition for a function or variable that has been declared in your code. This often happens when you declare a function in a header file but forget to provide its implementation in a corresponding source file. To fix this error, ensure that all declared functions are defined, and that your project settings include all necessary object files and library dependencies.

Here’s an example scenario and how you can resolve it:

Example Scenario


    // File: example.h
    void myFunction(); // Function declaration

    // File: main.cpp
    #include "example.h"

    int main() {
        myFunction(); // Calling the function
        return 0;
    }

    // File: example.cpp
    #include "example.h"
    void myFunction() { // Function definition
        // Function implementation
    }
    

Make sure that all files are included in your project and the source files where the functions are defined are being compiled and linked.


unresolved external symbol LNK2019 MSVC C++ linker errors function definition function declaration