gpt4 book ai didi

c++ - GoogleTest 仅在定义在 .h 文件中时有效

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:06 26 4
gpt4 key购买 nike

我花了一整天的时间试图让 GoogleTest 在 Visual Studio 2013 中工作。最后,让它“工作”,但只有当函数的定义放在 .h 文件中时它才工作。使用单独编译,如=:

// simplemath.h
#include <cmath>
double cubic(double d);

// simple.cpp
#include "simplemath.h"
double cubic(double d)
{
return pow(d, 3);
}

// unittest_SimpleMath.cpp
#include "gtest/gtest.h"
#include "simplemath.h"
TEST(testMath, myCubeTest)
{
EXPECT_EQ(1000, cubic(10));
}

产生以下错误:

1>------ Build started: Project: unittest_SimpleMath, Configuration: Release Win32 ------
1> unittest_SimpleMath.cpp
1>unittest_SimpleMath.obj : error LNK2001: unresolved external symbol "double __cdecl cubic(double)" (?cubic@@YANN@Z)
1>C:\Users\alex\Documents\Visual Studio 2013\Projects\SimpleMath\Release\unittest_SimpleMath.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

编辑:忘记提及一件重要的事情。我按照教程 http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php弄清楚如何配对 Gtest 和 VS2013。我的解决方案结构与 desc 相同。在教程中 - 3 个项目。

最佳答案

看来您正在使用不同的项目进行测试。您应该在具有 cubic 函数的同一项目中使用测试。或者从您的 cubic 代码创建 lib 并将其链接到测试项目中。您遇到的错误与 gtest 没有任何关系。你只是没有将你的 cpp 文件编译成一个可以在测试项目中使用的目标文件。

此外,请确保您在测试项目中链接了 gtest_main*.lib(此处有星号,因为它有几个名称,您应该选择所需的任何内容),因为您没有 main 用于它(或者你没有显示它)。

关于c++ - GoogleTest 仅在定义在 .h 文件中时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19785017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com