[英]Unresolved externals with google test
I have a project I am trying to add google-test
unit testing to.我有一个项目,我正在尝试向其中添加
google-test
单元测试。 It is structured like so:它的结构如下:
VM (project)虚拟机(项目)
some source files
BytecodePrograms.h
VMTest (project, made by add project -> google test -> link dynamically, test VM
) VMTest (项目,通过添加项目 -> 谷歌测试 -> 动态链接,测试
VM
)
pch.h
test.cpp
I added my VM
project as an include directory in VMTest
properties -> c/c++ -> general -> additional include directories我在
VMTest
属性 -> c/c++ -> 常规 -> 附加包含目录中添加了我的VM
项目作为包含目录
contents of test.cpp
are: test.cpp
内容是:
#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.但是,我的
VM
项目定义了我的WVM
类并使用它,并且可以构建和运行, BytecodePrograms.h
包括VM.h
,它具有此类的声明。
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.我觉得这一定是我在 Visual Studio 中设置项目的一个问题,但我不知道。 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.
我已经连续搜索了 2 天,发现其他人也有类似的问题,但他们的解决方案似乎对我不起作用。
Any ideas?有任何想法吗? Thanks.
谢谢。
I found a solution here: https://stackoverflow.com/a/19709712/8488701我在这里找到了一个解决方案: 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.与史蒂夫的建议类似,除了不是创建一个全新的项目,我使用构建后事件将我的项目构建到库,然后将 google test 链接到该库。 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.
与 Steve 的解决方案相比,它的优势在于您根本不必修改主项目,您仍然可以在其上构建单元测试项目。
This is a link error indicating that it found the .h file but couldn't find the actual implementation found in the .cpp.这是一个链接错误,表明它找到了 .h 文件,但找不到在 .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.
如果您有一个 LIB 项目,那么 Visual Studio 可能会将编译到 LIB 项目中的 cpp 代码添加到您的 EXE 项目中。
To incorporate the code into the test project, you have two options.要将代码合并到测试项目中,您有两个选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.