0

I just started a small console app in VS2019 and added a Google Test project. The program itself compiles and runs fine, however, if I move function code from the header to the cpp-file, I get the LNK2019 and LNK1120 errors:

Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: void __cdecl MyClass::someFunction(void)" (?someFunction@MyClass@@QEAAXXZ) referenced in function "private: virtual void __cdecl MyTests_basicTest_Test::TestBody(void)" (?TestBody@MyTests_basicTest_Test@@EEAAXXZ) Tests Tests.obj 1
Error LNK1120 1 unresolved externals Tests Tests.exe 1

MyClass.h:

#pragma once
class MyClass
{
public:
  void someFunction();
};

MyClass.cpp:

#include "MyClass.h"
void MyClass::someFunction() {
  // do something
}

Main.cpp:

#include "MyClass.h"

int main()
{
  MyClass mc;
  mc.someFunction();
}

Tests.cpp:

#include "pch.h"
#include "../TestApp/MyClass.h"

TEST(MyTests, basicTest) {
  MyClass mc;
  mc.someFunction();
  EXPECT_EQ(1, 1);
}

I could just put all of the code in the header file of course, but I don't think that is what I'm supposed to do, so I would greatly appreciate some help on this.

Edit: I feel like I'm one step closer (though it is only a shot in the dark): I added the directory that contains all the *.obj files under [Tests Project] -> "Properties" -> "Configuration Properties" -> "Linker" -> "Input" -> "Additional Dependencies": D:\Application\x64\Debug

However, now I get the following error:

LNK1104 cannot open file 'D:\Application\x64\Debug.obj'

And indeed, the directory contains all kinds of obj-files but not Debug.obj. :/

Edit 2: Solved! I followed this link under "To link the tests to the object or library files". Important that you really just enter the base name of the file itself without the ".obj".

Markstar
  • 639
  • 9
  • 24
  • How are you building your tests? – loic.lopez Jul 02 '21 at 08:18
  • Do you know how the build process in C/C++ works? I think you are not linking against any library to test, also you cannot test an executable like you created – RoQuOTriX Jul 02 '21 at 08:22
  • @loic.lopez: I open the Test Explorer and press "Run All Tests In View". – Markstar Jul 02 '21 at 08:32
  • @RoQuOTriX: No, not really. I know that the files are linked and intermediary obj-files are created, but not much more than that. What puzzles me that it works when I have all the code in the header and only fails once I move it outside. – Markstar Jul 02 '21 at 08:34

0 Answers0