Google 测试不适用于解决方案中其他项目中的 class
Google tests don't work with class in other project in solution
我正在尝试测试我的简单 class 构建(它具有良好的构造函数)。
文件 TestClass.h
#pragma once
class TestClass
{
public:
TestClass();
};
文件TestClass.cpp
#include "TestClass.h"
TestClass::TestClass()
{
}
然后我在我的解决方案中添加新项目:tests
。该项目仅包含一个文件。
文件 test.cpp
#include <gtest/gtest.h>
#include "../Example/TestClass.h"
TEST(Test0, all)
{
EXPECT_EQ(true, true);
}
/*
TEST(Test1, part1)
{
TestClass t;
}*/
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
如果我构建该代码 - 一切都很好(以及所有测试)。但是如果我取消注释注释块 - 我有这样的错误输出:
1>------ Rebuild All started: Project: Example, Configuration: Debug Win32 ------
1> TestClass.cpp
1> Example.vcxproj -> d:\Alex\documents\visual studio 2015\Projects\tests\Debug\Example.lib
2>------ Rebuild All started: Project: tests, Configuration: Debug Win32 ------
2> test.cpp
2>test.obj : error LNK2019: unresolved external symbol "public: __thiscall TestClass::TestClass(void)" (??0TestClass@@QAE@XZ) referenced in function "private: virtual void __thiscall Test1_part1_Test::TestBody(void)" (?TestBody@Test1_part1_Test@@EAEXXZ)
2>d:\Alex\documents\visual studio 2015\Projects\tests\Debug\tests.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
我将 gtest.lib
文件添加到 tests
项目的链接器输入的依赖项中。
我将 Example
项目添加为 tests
项目的依赖项。
我将 Example
项目构建为静态库 (.lib)。
如果我从 TestClass
中删除构造函数 - 一切都会好起来的。
我的项目你可以找到 here.
我在 Windows 10 Pro x64.
上使用带有 Update 3 的 Microsoft Visual Studio 2015
我的 Google 测试框架版本是 1.8.0。
如何解决这个编译问题?我真的需要这个构造函数。
我可以解决这个问题:
我手动添加 buildDir
(对我来说是 $(SolutionDir)$(Configuration)\
):
tests
属性 -> 配置属性 -> 链接器 -> 常规 -> 附加库目录
然后我以这种方式手动添加 Example.lib
依赖项:
tests
属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项
这正是我现在遇到的问题。解决方法如下:
确保添加参考:在测试项目下, 添加对 源项目 的引用。
设置项目的配置属性包含TestClass.cpp和TestClass.h作为静态库:在VS 2015中,它在项目 -> 属性 -> 配置属性 -> 常规 -> 项目默认值 -> 配置类型。另外,注释掉Source Project.
中的main函数
希望对您有所帮助。:)
我正在尝试测试我的简单 class 构建(它具有良好的构造函数)。
文件 TestClass.h
#pragma once
class TestClass
{
public:
TestClass();
};
文件TestClass.cpp
#include "TestClass.h"
TestClass::TestClass()
{
}
然后我在我的解决方案中添加新项目:tests
。该项目仅包含一个文件。
文件 test.cpp
#include <gtest/gtest.h>
#include "../Example/TestClass.h"
TEST(Test0, all)
{
EXPECT_EQ(true, true);
}
/*
TEST(Test1, part1)
{
TestClass t;
}*/
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
如果我构建该代码 - 一切都很好(以及所有测试)。但是如果我取消注释注释块 - 我有这样的错误输出:
1>------ Rebuild All started: Project: Example, Configuration: Debug Win32 ------
1> TestClass.cpp
1> Example.vcxproj -> d:\Alex\documents\visual studio 2015\Projects\tests\Debug\Example.lib
2>------ Rebuild All started: Project: tests, Configuration: Debug Win32 ------
2> test.cpp
2>test.obj : error LNK2019: unresolved external symbol "public: __thiscall TestClass::TestClass(void)" (??0TestClass@@QAE@XZ) referenced in function "private: virtual void __thiscall Test1_part1_Test::TestBody(void)" (?TestBody@Test1_part1_Test@@EAEXXZ)
2>d:\Alex\documents\visual studio 2015\Projects\tests\Debug\tests.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
我将 gtest.lib
文件添加到 tests
项目的链接器输入的依赖项中。
我将 Example
项目添加为 tests
项目的依赖项。
我将 Example
项目构建为静态库 (.lib)。
如果我从 TestClass
中删除构造函数 - 一切都会好起来的。
我的项目你可以找到 here.
我在 Windows 10 Pro x64.
上使用带有 Update 3 的 Microsoft Visual Studio 2015
我的 Google 测试框架版本是 1.8.0。
如何解决这个编译问题?我真的需要这个构造函数。
我可以解决这个问题:
我手动添加 buildDir
(对我来说是 $(SolutionDir)$(Configuration)\
):
tests
属性 -> 配置属性 -> 链接器 -> 常规 -> 附加库目录
然后我以这种方式手动添加 Example.lib
依赖项:
tests
属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项
这正是我现在遇到的问题。解决方法如下:
确保添加参考:在测试项目下, 添加对 源项目 的引用。
设置项目的配置属性包含TestClass.cpp和TestClass.h作为静态库:在VS 2015中,它在项目 -> 属性 -> 配置属性 -> 常规 -> 项目默认值 -> 配置类型。另外,注释掉Source Project.
中的main函数
希望对您有所帮助。:)