简体   繁体   中英

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.

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.

This is a link error indicating that it found the .h file but couldn't find the actual implementation found in the .cpp. If you have a LIB project then visual studio may take the cpp code compiled into the LIB project and add it to your EXE project.

To incorporate the code into the test project, you have two options.

  1. You can add the .cpp files in your VM Project to your Test project as well, but this is not usually done.
  2. Instead, if your VM project is now an EXE I would recommend creating a new project called VMLib as a LIB project and then adding that project to both the test project and the VM EXE project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM