GoogleTest仅在定义在.h文件中时才有效
GoogleTest only work when definition is in .h file
我花了一整天的时间试图让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:UsersalexDocumentsVisual Studio 2013ProjectsSimpleMathReleaseunittest_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
代码制作库并将其链接到测试项目中。您遇到的错误与 gtest 没有任何关系。您只是没有将 cpp 文件编译为可以在测试项目中使用的对象文件。
另外,请确保您在测试项目中链接了gtest_main*.lib
(星号,因为它有几个名称,您应该选择您需要的任何名称),因为您没有main
(或者您没有显示它)。
相关文章:
- 自定义先决条件对移动分配运算符有效吗
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 此递归模板类型定义是否有效C++?
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)
- 通过自定义分配器和删除器有效使用shared_ptr
- 当程序从该函数调谐器时,指向在函数中声明和定义的某些 C 字符串的指针不再有效.为什么?
- 如何确定以浮点数、双精度位数为单位的有效宽度:是否有标准定义?
- C - 一个函数声明,多个定义,并且仍然有效
- 静态常量声明,变量的constexpr定义,有效的c ++?
- 将流定义为私有类变量似乎在Linux下有效,但在WindowsVisualStudio下无效
- 未解析的外部符号错误,仅在标头中定义函数时有效
- 为什么仅在返回通用类中的自定义类型时才有效constexpr成员函数
- 是无定义的类声明,有效为模板参数
- std::unique_ptr的自定义deleter是手动调用析构函数的有效位置吗
- 为什么在头文件中定义类是有效的,而不是函数
- 在枚举类上使用“reinterpret_cast” - 有效或未定义的行为
- STL 集自定义排序函数在 Qunicy 2005 中有效,但在 MS Studio 2010 中不起作用
- GoogleTest仅在定义在.h文件中时才有效
- 定义内省C++类层次结构描述的有效方法