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".