Search code examples
c++googletest

Unresolved externals with google test


I have a project I am trying to add google-test unit testing to. It is structured like so:

VM (project)

some source files

BytecodePrograms.h

VMTest (project, made by add project -> google test -> link dynamically, test VM)

pch.h

test.cpp

I added my VM project as an include directory in VMTest properties -> c/c++ -> general -> additional include directories

contents of test.cpp are:

#include "pch.h"
#include "BytecodePrograms.h"

TEST(TestCaseName, TestName) {
  EXPECT_EQ(8, VMFibonacciImp(6));
  EXPECT_TRUE(true);
}

If I build, I get the following errors

Error   LNK2019 unresolved external symbol "public: __thiscall WVM::WVM(void)" (??0WVM@@QAE@XZ) referenced in function "int __cdecl VMFibonacciImp(int)" (?VMFibonacciImp@@YAHH@Z)  WVMTest C:\Users\WadeMcCall\source\repos\Virtual Machine Visual Scripting\WVMTest\test.obj  1   
Error   LNK2019 unresolved external symbol "public: __thiscall WVM::~WVM(void)" (??1WVM@@QAE@XZ) referenced in function "int __cdecl VMFibonacciImp(int)" (?VMFibonacciImp@@YAHH@Z) WVMTest C:\Users\WadeMcCall\source\repos\Virtual Machine Visual Scripting\WVMTest\test.obj  1   
Error   LNK2019 unresolved external symbol "public: int __thiscall WVM::interpret(class std::vector<int,class std::allocator<int> >)" (?interpret@WVM@@QAEHV?$vector@HV?$allocator@H@std@@@std@@@Z) referenced in function "int __cdecl VMFibonacciImp(int)" (?VMFibonacciImp@@YAHH@Z)  WVMTest C:\Users\WadeMcCall\source\repos\Virtual Machine Visual Scripting\WVMTest\test.obj  1   

However, my VM project defines my WVM class and uses it and can build and run and BytecodePrograms.h includes VM.h which has the declaration for this class.

I feel like this must just be a problem with the set up of my projects in Visual Studio somehow, but I have no idea. I have been googling for 2 days straight and have found other people with similar problems but their solution never seems to work for me.

Any ideas? Thanks.


Solution

  • I found a solution here: https://stackoverflow.com/a/19709712/8488701

    Similar to what Steve suggested, except instead of creating a whole new project, I use a post-build event to build my project to a library and then link google test to that. The advantage of this over Steve's solution is that you don't have to modify your main project at all and you can still build a unit testing project on top of it.