LNK2019: unresolved external symbol in VS unit-testing

后端 未结 4 1803
忘了有多久
忘了有多久 2021-02-19 19:40

I get the error as stated in the title. I ensured the following:
- The Include directory, include library and additional include directory are set correctly
- In the pro

相关标签:
4条回答
  • 2021-02-19 20:04

    I know this is very late but let me explain why including the .cpp file fixes the issue.

    #include "../LifeLib/StornoTafel.h"
    
    ...
    
    LifeLib::StornoTafel stornoTafel_; // Throws unresolved external symbol
    

    Your unit test project exists outside of your application, you have provided references to the header files which provide the declarations of your types and methods but not their definitions.

    By placing references to the .cpp files the compiler can actually get the declarations of these signatures:

    #include "../LifeLib/StornoTafel.h"
    #include "../LifeLib/StornoTafel.cpp"
    
    ...
    
    LifeLib::StornoTafel stornoTafel_; // Works perfectly
    

    The simplest solution is to simply duplicate your header file reference and change .h to .cpp

    0 讨论(0)
  • 2021-02-19 20:06

    I found the answer by myself: I have to include the .cpp file.
    So #include "../LifeLib/StornoTafel.cpp" fixes the error. However, I have no idea why.

    0 讨论(0)
  • 2021-02-19 20:07

    I had similar problem and fixed it by adding the output .obj files to the dependencies of the test project.

    refer to MSDN document - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef

    0 讨论(0)
  • 2021-02-19 20:23

    In my experience two things make this error show up, either in the software you are using you didn't properly include it in your project, therefor the link error shows up, or maybe there is a mix between the versions of both your project and the one you try to include. By this i mean, check if they are both 64 or 32. if they are not the same this error will show up. These are the things i know can cause this, it can be something else.

    0 讨论(0)
提交回复
热议问题