51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 7862|回复: 0
打印 上一主题 下一主题

[原创] 安装Google Test碰到的问题,供大家参考

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-2-23 15:12:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
安装:
下载Google C++ Testing Framework,解压...
VC2005:
     直接打开msvc\gtest.vcproj或msvc\gtest.sln,直接编译即可。

详细步骤如下:

1,编译生成gtest.lib和gtestd.lib静态库,分别对应Release和Debug版本.(附件中有)

2,在VS2005的工程属性中包含gtest的include和lib目录.(附件中有)

3,使用VS2005建立空项目,工程属性的设置:
注意:特别要注意的是建立空项目后必须要先添加一个类才能设置前面两项(即代码生成的那两项)

工程属性设置                        Release           Debug

C/C++->代码生成->运行时库         多线程(/MT)     多线程调试(/MTd)

C/C++->代码生成->基本运行时检查     默认值      两者(/RTC1,等同于 /RTCsu)

链接器->输入->附加依赖项           Gtest.lib     Gtestd.lib


4,输入MyMath.h和MyMath.cpp源文件如下:
MyMath.h:

class MyMath
{
public:
    static int Add(int num1, int num2);
};


MyMath.cpp:

#include "MyMath.h"

int MyMath::Add(int num1, int num2)
{
    return num1 + num2;
}


5,建立测试文件MyMathTest.cpp如下:

#include "MyMath.h"
#include <gtest/gtest.h>

TEST(MyMathTest, Positive)
{
    EXPECT_EQ(3, MyMath::Add(1, 2));
}


6,建立主执行文件main.cpp如下:

#include <iostream>
#include <gtest/gtest.h>     --首先#include <gtest/gtest.h>,当然工程的头文件路径要设置正确否则会报第八步的错误

int main(int argc, char **argv) {
    std::cout << "Running main() from gtest_main.cc\n";

    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}


7,编译执行即可,输出如下:

Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyMathTest
[ RUN      ] MyMathTest.Positive
[       OK ] MyMathTest.Positive
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran.
[  PASSED  ] 1 test.
请按任意键继续. . .


8如果编译出现的错误,fatal error C1083: Cannot open include file: \' Gtest.h \': No such file or directory
解决方案:
a,工具--选项--VC++目录--显示以下内容的目录(包含文件)--(新建一行,选择自己include文件所在的目录即可)E:\程序安装包\google test\gtest-1.2.1\include
b,工具--选项--VC++目录--显示以下内容的目录(包库文件)--(新建一行,选择自己库文件所在的目录)E:\程序安装包\google test\gtest-1.2.1\msvc\Release
问题就解决了,出现了第七步的结果。如果第8步的设置在新建项目时设置好了后面就不会报错了。

[ 本帖最后由 卡朵 于 2009-2-23 15:16 编辑 ]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-6-9 17:37 , Processed in 0.081924 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表