c++ - 谷歌测试 Unresolved external 问题

标签 c++ googletest

我有一个项目正在尝试添加 google-test 单元测试。它的结构如下:

虚拟机(项目)

some source files

BytecodePrograms.h

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

pch.h

test.cpp

我在 VMTest properties -> c/c++ -> general -> additional include directories 中添加了我的 VM 项目作为包含目录

test.cpp 的内容是:

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

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

如果我构建,我会收到以下错误

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   

但是,我的 VM 项目定义了我的 WVM 类并使用它并且可以构建和运行并且 BytecodePrograms.h 包括 VM .h 包含此类的声明。

我觉得这一定是我在 Visual Studio 中设置项目的问题,但我不知道。我连续 2 天都在谷歌上搜索,发现其他人也有类似的问题,但他们的解决方案似乎对我没有用。

有什么想法吗?谢谢。

最佳答案

我在这里找到了解决方案:https://stackoverflow.com/a/19709712/8488701

与 Steve 的建议类似,除了没有创建一个全新的项目,我使用构建后事件将我的项目构建到一个库,然后将 google test 链接到该库。与 Steve 的解决方案相比,它的优势在于您根本不必修改主项目,您仍然可以在其上构建单元测试项目。

关于c++ - 谷歌测试 Unresolved external 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049330/

相关文章:

c++ - Eigen 矩阵库系数明智的操作

c++ - += 与 C++ 中的字符串

c++ - strncpy 运行时检查失败 #2 错误 c++

c++ - 从模拟对象中读出期望

c++ - 链接到多个 .obj 以对控制台应用程序进行单元测试

c++ - 使用google test和google mock比较 float 组

c++ - while循环中的多个条件使用&&

c++ - 可扩展的 UDP 服务器

c++ - GTEST : Identify before and after a SetUp and TearDown respectively

c++ - googletest - 如何在输出结果时将我的参数化测试合并为一个?