0

I'm trying to write unit tests for my classes but I'm getting "unresolved external symbol." There are no syntax errors and I have tried looking up the cause on the official Microsoft website but unfortunately it can be caused for a million different reasons. Any suggestions on what I can do or what might be causing it? This is my code:

// Example.h file
#pragma once
#include <iostream>

using namespace std;

class Example {
public:
  int addTwoNumbers(int first, int second);
};
// Example.cpp file
#include "Example.h"
#include <iostream>

using namespace std;

int Example::addTwoNumbers(int first, int second) {
  return first + second;
}
// Test.cpp file
#include "pch.h"
#include "../Project/Example.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Tests
{
  TEST_CLASS(Tests)
  {
  public:
    TEST_METHOD(UnitTest1)
    {
      Example e;
      Assert::AreEqual(4, e.addTwoNumbers(2, 2)); // this line is causing issues
    }
  };
}

The exact error that I'm getting:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: int __thiscall Example::addTwoNumbers(int,int)" (?addTwoNumbers@Example@@QAEHHH@Z) referenced in function "public: void __thiscall Tests::Tests::UnitTest1(void)" (?UnitTest1@Tests@1@QAEXXZ)   Tests   C:\Users\me\source\repos\Tests\Tests.obj    1   
Kau
  • 91
  • 2
  • 8
  • In your Test project how are you compiling `Example.cpp`? Or is Example.cpp in a static library? Or have you included Example.obj in your linker setting for the Test project? – drescherjm Nov 06 '20 at 20:44
  • Please provide the full error with the symbol that is unresolved. Things to check: if program and test are separate projects, ensure the link tab contains the program/library in the list. Same static or dynamic C++. – un-CharlieH Nov 06 '20 at 20:55
  • @drescherjm I have added the Project as a reference by clicking on Tests and then on Add Reference... Is there anything else that I need to do? I have also updates the post with the full error that I'm getting. Thank you! – Kau Nov 06 '20 at 22:16
  • @un-CharlieH I have updated the code with the original error. The project and test are different projects but I have added a reference to project in test. How do i check if there are both static or dynamic? Thank you! – Kau Nov 06 '20 at 22:18
  • I am not sure references help in native c++ that is more of a .NET / CLR concept, Related: [https://stackoverflow.com/questions/40481799/adding-reference-to-native-visual-c-project](https://stackoverflow.com/questions/40481799/adding-reference-to-native-visual-c-project) – drescherjm Nov 06 '20 at 22:19
  • So what would be an alternative? Is there an easier way to unit test? – Kau Nov 06 '20 at 22:22
  • I gave you several of the alternatives in my first comment. – drescherjm Nov 06 '20 at 22:22
  • I'm not sure what any of those things mean. – Kau Nov 06 '20 at 22:24
  • The easiest would be to add Example.cpp to your test project by right clicking on the project in Solution Manager and select Add ->existing item – drescherjm Nov 06 '20 at 22:25
  • Alright llet me try that. – Kau Nov 06 '20 at 22:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224223/discussion-between-drescherjm-and-kau). – drescherjm Nov 06 '20 at 22:33
  • @kau: You need to add an import for the test project and export for the Example class in the library. See the [import-and-exporting](https://learn.microsoft.com/en-us/cpp/build/importing-and-exporting?view=msvc-160) topic or the [dllexport SO post](https://stackoverflow.com/questions/48977138/using-declspec-dllexport). – un-CharlieH Nov 09 '20 at 17:47

1 Answers1

0

You could check if the following steps are included. You could refer to this link about Write unit tests for C/C++ in Visual Studio.

  1. Add Reference to UnitTest.

  2. Link to object or library files: Properties->Linker->Input->Additional Dependencies->input ..\ProjectName\Debug\*.obj

  3. Add #include directives for header files: #include "../Project1/Example.h"

If the lnk2019 still appears, try to add the following code to Example.cpp.

#include<iostream>
#include "Example.h"
using namespace std;

int Example::addTwoNumbers(int first, int second) {
    return first + second;
}

int main()
{
    return 0;
}
Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7