【问题标题】:Google tests don't work with class in other project in solutionGoogle 测试不适用于解决方案中其他项目中的类
【发布时间】:2017-04-14 14:06:09
【问题描述】:

我正在尝试测试用于构建的简单类(它具有良好的构造函数)。
档案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>----- 重建全部启动:项目:示例,配置:调试 Win32 ------
1> 测试类.cpp
1> Example.vcxproj -> d:\Alex\documents\visual studio 2015\Projects\tests\Debug\Example.lib
2>------ 重建全部启动:项目:测试,配置:调试 Win32 ------
2> 测试.cpp
2>test.obj : 错误 LNK2019: 未解析的外部符号“public: __thiscall TestClass::TestClass(void)” (??0TestClass@@QAE@XZ) 在函数“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 : 致命错误 LNK1120: 1 unresolved externals
==========全部重建:1成功,1失败,0跳过==========

我将gtest.lib 文件添加到tests 项目的链接器输入的依赖项中。
我添加了Example 项目作为tests 项目的依赖项。
我将 Example 项目构建为静态库 (.lib)。
如果我从 TestClass 中删除构造函数 - 一切都会好起来的。
我的项目你可以找到here。
我在 Windows 10 Pro x64 上使用 Microsoft Visual Studio 2015 和 Update 3。
我的 Google 测试框架版本是 1.8.0。

如何解决这个编译问题?我真的需要这个构造函数。

【问题讨论】:

    标签: visual-studio unit-testing visual-c++ visual-studio-2015 googletest


    【解决方案1】:

    这正是我现在遇到的问题。解决方法如下:

    • 确保添加 Reference:在 Test Project 下,添加对 Source Project 的引用>。

    • 设置项目的Configuration Properties包含TestClass.cpp和TestClass.h为静态库:在 VS 2015 中,它位于 Project -> Properties -> Configuration Properties -> General -> Project Defaults -> Configuration Type 下。另外,将Source Project中的main函数注释掉。

    希望对你有帮助。:)

    【讨论】:

      【解决方案2】:

      我可以解决这个问题:
      我以这种方式手动添加buildDir(对我来说是$(SolutionDir)$(Configuration)\):
      tests properties -> Configuration Properties -> Linker -> General -> Additional Library 目录
      我手动添加 Example.lib 依赖项:
      tests 属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多