[Solved-5 Solutions] What is an undefined reference/unresolved external symbol error and how to fix it ?



Error description:

    • What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?

    Solution 1:

    • The specified errors occur during this last stage of compilation, most commonly referred to as linking. It basically means that we compiled a bunch of implementation files into object files or libraries and now we want to get them to work together.

    Common causes include:

    • Failure to link against appropriate libraries/object files or compile implementation files
    • Declared and undefined variable or function.
    • Common issues with class-type members
    • Template implementations not visible.
    • Symbols were defined in a C program and used in C++ code.
    • Incorrectly importing/exporting methods/classes across modules/dll. (MSVS specific)
    • Circular library dependency
    • undefined reference to `WinMain@16'
    • Interdependent library order
    • Multiple source files of the same name
    • Mistyping or not including the .lib extension when using the #pragma (MSVC)
    • Problems with template friends
    • Inconsistent UNICODE definitions

    Solution 2:

    • It can also happen that we forget to add the file to the compilation, in which case the object file won't be generated. In gcc we'd add the files to the command line. In MSVS adding the file to the project will make it compile it automatically (albeit files can, manually, be individually excluded from the build).

    Solution 3:

    • The order in which libraries are linked DOES matter if the libraries depend on each other. In general, if library A depends on library B, then libA MUST appear before libB in the linker flags.

    Solution 4:

    • The function (or variable) void foo() is defined in a C program and you attempt to use it in a C++ program:
    void foo();
    int main()
    {
        foo();
    }
    
    click below button to copy the code. By c++ tutorial team
    • The C++ linker expects names to be mangled, so you have to declare the function as:
    extern "C" void foo();
    int main()
    {
        foo();
    }
    
    click below button to copy the code. By c++ tutorial team
    • Equivalently, instead of being defined in a C program, the function (or variable) void foo() is defined in C++ but with C linkage:
    extern "C" void foo();
    
    click below button to copy the code. By c++ tutorial team
    • and we attempt to use it in a C++ program with C++ linkage.
    • If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;
    extern "C" {
        #include "cheader.h"
    }
    
    click below button to copy the code. By c++ tutorial team

    Solution 5:

    • We will be able to get rid of an unresolved external error in Visual Studio 2012 just by recompiling the offending file. When we re-build, the error goes away.
    • This usually happens when two (or more) libraries have a cyclic dependency. Library A attempts to use symbols in B.lib and library B attempts to use symbols from A.lib. Neither exist to start off with. When we attempt to compile A, the link step will fail because it can't find B.lib. A.lib will be generated, but no dll. We then compile B, which will succeed and generate B.lib. Re-compiling A will now work because B.lib is now found.

    Related Searches to What is an undefined reference/unresolved external symbol error and how to fix it ?