[c++] Unresolved external symbol in object files

During coding in Visual Studio I got an unresolved external symbol error and I've got no idea what to do. I don't know what's wrong. Could you please decipher me? Where should I be looking for what kind of errors?

1>Form.obj : error LNK2019: unresolved external symbol "public: class Field * __thiscall Field::addField(class Field *)" (?addField@Field@@QAEPAV1@PAV1@@Z) referenced in function "public: void __thiscall Form::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Form@@QAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::prompt(void)" (?prompt@Field@@UAEXXZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getName(void)" (?getName@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getType(void)" (?getType@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::describe(void)" (?describe@Field@@UAEXXZ)
1>C:\Users\tomy\Documents\Visual Studio 2010\Projects\zapoctovkac++\Debug\zapoctovkac++.exe : fatal error LNK1120: 6 unresolved externals

This question is related to c++ visual-studio visual-c++ unresolved-external

The answer is


Make sure that you are not trying to overload the insertion or extraction operators as inline functions. I had this problem and it only went away when i removed that keyword.


One possible cause of this linker error can also be inline functions that are declared but not defined in a header file that is then included somewhere else. Inline functions have to be defined in every translation unit they are used in.


POINTERS

I had this problem and solved it by using pointer. I see that this wasn't your issue but I thought I'd mention it because I sure wish it had been here when I saw this an hour ago. My issue was about declaring a static member variable without defining it (the definition needed to come after some other set ups) and of course a pointer doesn't need a definition. Equally elementary mistake :P


My issue was: I had to do forward declaration of the class whose ctor was "unresolved external".

In the file where I got the error, I had to put something like this:

#include "ClassB" 

class ClassB; // this solved the problem

class ClassA{
    void foo(){
        ClassB* tmp = new ClassB();
        // ...
    }
};

Of course, my project is much more complicated and this is just a snippet example. Also when using namespaces, declare them as well.


In my case, I needed add the function name to the DEF file.

LIBRARY   DEMO
EXPORTS
   ExistingFunction   @1
   MyNewFunction      @2

I've just seen the problem I can't call a function from main in .cpp file, correctly declared in .h file and defined in .c file. Encountered a linker error. Meanwhile I can call function from usual .c file. Possibly it depends on call convention. Solution was to add following preproc lines in every .h file:

#ifdef __cplusplus
extern "C"
{
#endif

and these in the end

#ifdef __cplusplus
}
#endif

This error can be caused by putting the function definitions for a template class in a separate .cpp file. For a template class, the member functions have to be declared in the header file. You can resolve the issue by defining the member functions inline or right after the class definition in the .h file.

For example, instead of putting the function definitions in a .cpp file like for other classes, you could define them inline like this:

template<typename T>
MyClassName {
  void someFunction() {
    // Do something
    ...
  }
  void anotherFunction() {
    // Do something else
    ...
  }
}

Or you could define them after the class definition but in the same file, like this:

template<typename T>
MyClassName {
  void someFunction();
  void anotherFunction();
}

void MyClassName::someFunction() {
  // Do something
  ...
}
void MyClassName::anotherFunction() {
  // Do something else
  ...
}

I just thought I'd share that since no one else seems to have mentioned template classes. This was the cause of the error in my case.


Make sure you decorate your header files with

#ifndef YOUR_HEADER_H
#define YOUR_HEADER_H

// your header code here

#endif

Bad things -including this- can happen if you don't


Check you are including all the source files within your solution that you are referencing.

If you are not including the source file (and thus the implementation) for the class Field in your project it won't be built and you will be unable to link during compilation.

Alternatively, perhaps you are using a static or dynamic library and have forgotten to tell the linker about the .libs?


I'm doing some C++ for the first time in a long time, and I'm getting this error when I forget to add the ClassName:: prefix for the function definition, since this is a little unique to C++. So remember to check for that too!


It looks to be missing a library or include, you can try to figure out what class of your library that have getName, getType etc ... and put that in the header file or using #include.

Also if these happen to be from an external library, make sure you reference to them on your project file. For example, if this class belongs to an abc.lib then in your Visual Studio

  1. Click on Project Properties.
  2. Go to Configuration Properties, C/C++, Generate, verify you point to the abc.lib location under Additional Include Directories. Under Linker, Input, make sure you have the abc.lib under Additional Dependencies.

In addition to the excellent answer by Chris Morris above, I found a very interesting way you can receive this same fault if you are calling to a virtual method that hasn't been set to pure but doesn't its own implementation. It is the exact same reason (the compiler can't find an implementation of the method and therefore crooks), but my IDE did not catch this fault in the least bit.

for example, the following code would get a compilation error with the same error message:

//code testing an interface
class test
{
   void myFunc(); 
}

//define an interface
class IamInterface
{
    virtual void myFunc();
}

//implementation of the interface
class IamConcreteImpl
{
    void myFunc()
    {
       1+1=2;
    }
}

However, changing IamInterface myFunc() to be a pure virtual method (a method that "must" be implemented, that than a virtual method which is a method the "can" be overridden) will eliminate the compilation error.

//define an interface
class IamInterface
{
    virtual void myFunc() = 0;
}

Hopes this helps the next StackOverFlow person stepping through code!


I had an error where my project was compiled as x64 project. and I've used a Library that was compiled as x86.

I've recompiled the library as x64 and it solved it.


I've just had the same error and I manage to avoid it by replacing ; with {} in the header file.

#ifndef XYZ_h
#define XYZ_h
class XYZ
{
    public:
    void xyzMethod(){}
}
#endif

When it was void xyzMethod(); it didn't want to compile.


Yet another possibility to check, it was my problem this time.

I had added the function to the library, and included the library's output folder in the search path.

But I also had a folder with an older version of the library listed before, so VS was using the old library, and of course not finding the new function.


Just spent a couple of hours to find that the issue was my main file had extension .c instead of .cpp

:/


I came here looking for a possible explanation before taking a closer look at the lines preceding the linker error. It turned out to have been an additional executable for which the global declaration was missing!


I just had a hard time with this. Everything was logically set up. I declared a constructor but didn't define it

class SomeClass
{
   SomeClass();  // needs the SomeClass::SomeClass(){} function defined somewhere, even here
}

I almost banged my head on my keyboard when I forgot something so elementary.


Yet another possible problem (that I just scratched my head about for some time):

If you define your functions as inline, they—of course!—have to be defined in the header (or an inline file), not a cpp.
In my case, they were in an inline file, but only because they were a platform specific implementation, and a cpp included this corresponding inl file… instead of a header. Yeah, s**t happens.

I thought I'd leave this here, too, maybe someone else runs into the same issue and finds it here.


I had the same link errors, but from a test project which was referencing another dll. Found out that after adding _declspec(dllexport) in front of each function which was specified in the error message, the link was working well.


I faced a similar issue and finally managed to solve it by adding __declspec(dllimport) to the declaration of the class:

// A.hpp
class __declspec(dllimport) A
{
   public: void myFunc();

   // Function declaration
};

A possible reason for the "Unresolved external symbol" error can be the function calling convention.

Make sure that all the source files are using same standard (.c or .cpp), or specify the calling convention.

Otherwise, if one file is a C file (source.c) and another file is a .cpp file, and they link to the same header, then the "unresolved external symbol" error will be thrown, because the function is first defined as a C cdecl function, but then C++ file using the same header will look for a C++ function.

To avoid the "Unresolved external symbol error", make sure that the function calling convention is kept the same among the files using it.


I believe most of the points regarding the causes and remedies have been covered by all contributors in this thread. I just want to point out for my 'unresolved external' problem, it was caused by a datatype defined as macro that gets substituted differently than expected, which results in that incorrect type being supplied to the function in question, and since the function with type is never defined, it couldn't have been resolved. In particular, under C/C++ -> Language, there is an attribute called 'Treat WChar_t As Built in Type, which should have been defined as 'No (/Zc:wchar_t-)' but did not in my case.


sometimes if a new header file is added, and this error starts coming due to that, you need to add library as well to get rid of unresolved external symbol.

for example:

#include WtsApi32.h

will need:

#pragma comment(lib, "Wtsapi32.lib") 

My issue was a sconscript did not have the cpp file defined in it. This can be very confusing because Visual Studio has the cpp file in the project but something else entirely is building.


What had caused it in my case:

I had a huge file Foo.cpp without a Foo.h. Foo.cpp began like this:

// ... 100 LOC here ...
namespace NS {
// ... 100 more LOC here ...
static int var;

I removed the "static" keyword and added a Foo.h with this:

extern int var;

Do you see the mistake?

I totally missed that var was originally defined in a namespace, because the namespace declaration was buried in other code. The fix is to change the extern like this:

namespace NS {
     extern int var;
}

See Linker Tools Error LNK2019 at MSDN, it has a detailed list of common problems that cause LNK2019.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to visual-c++

How to install Visual C++ Build tools? Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat) How to install numpy on windows using pip install? How to use _CRT_SECURE_NO_WARNINGS How to play or open *.mp3 or *.wav sound file in c++ program? What is C# equivalent of <map> in C++? The program can't start because MSVCR110.dll is missing from your computer error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj Identifier is undefined error LNK2001: unresolved external symbol (C++)

Examples related to unresolved-external

unresolved external symbol __imp__fprintf and __imp____iob_func, SDL2 What is an undefined reference/unresolved external symbol error and how do I fix it? Unresolved external symbol in object files