免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2676 | 回复: 0
打印 上一主题 下一主题

Hack gtest,使之可以在Dev C++(MinGW3.4.2)环境下使用 [复制链接]

论坛徽章:
1
水瓶座
日期:2014-03-20 18:21:14
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-28 12:19 |只看该作者 |倒序浏览
gtest是google开发的一个单元测试框架
在windows平台,gtest官方只提供两种编译方式:1、VS2005的工程文件;2、cygwin下使用autoconfig
对于使用纯windows版本gcc作为核心编译器的Dev C++或者Code::Block这些开发环境来说,gtest无法直接使用

我尝试过使用VS和cygwin编译出来的gtest库文件用于Dev C++(自带MinGW3.4.2),结果都是链接错误。随后便尝试使用Dev C++来手工编译gtest本身

准备工作:
将gtest的整个src和include目录拷贝到你的工作目录

编译过程:
  • 首先你要明确一个概念,gtest有两个库,一个是含有main函数的,一个是不含有main函数的,两者互斥,所以我们需要建立两个工程,分别编译出两个库文件出来
  • 首先建立gtest-all工程,工程的类型一定要是“静态库”。把src中除了gtest_main.c以外的所有文件都加入,然后注意在工程属性里面加入库文件路径,路径要求是工程目录本身、src目录、include目录,共三者
  • 编译的话,有一个文件会报错,我们主要就是hack这里:
    gtest.cc   808行 (这个行数有可能不准)
    struct timeval now;  这个结构
    它使用了一个函数 gettimeofday(&now, NULL);
    MinGW没有这个函数,出错就在这里
    我使用了一种方法把它替换掉,来源于此http://lists-archives.org/mingw-users/04934-missing-gettimeofday-function.html
    这个方法修改后的结果:
    • struct timeval now;
    •     FILETIME ft;
    •   long long *time64 = (long long *) &ft;
    •   GetSystemTimeAsFileTime (&ft);
    •   /* Convert from 100s of nanoseconds since 1601-01-01
    •    * to Unix epoch.
    •    */
    •   *time64 -= 116444736000000000LL;
    •   *time64 /= 10;
    •   now.tv_sec = *time64 / 1000000;
    •   now.tv_usec = *time64 % 1000000;
    • //  gettimeofday(&now, NULL);
    •   return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000;

    修改过后,所有的源文件就可以无错编译了,一切按照默认的静态库工程模式编译,就可以生成一个gtest-all.a的库文件了
  • 去掉gtest-all.c文件,加入gtest_main.c文件,然后将工程另存为gtest_main工程,这个就是另外一个库了。因为gtest.c文件已经改过了,所以现在这个新工程可以直接正常编译成功,生成一个gtest_main.a的库文件
  • OK,现在你就得到了两个gtest的库文件,一个用于自带main函数的测试用例,一个用于没有自带main函数的测试用例。新建一个Dev C++工程,类型是控制台程序加入gtest官方自带的sample1例子,三个源文件。然后把gtest的include路径包含到该工程属性里面,另外在 工程选项->参数->链接器 里面加入gtest_main.a库文件,因为sample1是不自带main函数的
    OK,现在我们可以编译该工程,就生成了一个sample1.exe的可执行文件,打开一个DOS窗口,运行它,就可以验证我们工作的结果了
    • C:\Users\zachary>F:\C\gtest\sample1.exe
    • Running main() from gtest_main.cc
    • [==========] Running 6 tests from 2 test cases.
    • [----------] Global test environment set-up.
    • [----------] 3 tests from FactorialTest
    • [ RUN      ] FactorialTest.Negative
    • [       OK ] FactorialTest.Negative (0 ms)
    • [ RUN      ] FactorialTest.Zero
    • [       OK ] FactorialTest.Zero (0 ms)
    • [ RUN      ] FactorialTest.Positive
    • [       OK ] FactorialTest.Positive (0 ms)
    • [----------] 3 tests from FactorialTest (6 ms total)
    • [----------] 3 tests from IsPrimeTest
    • [ RUN      ] IsPrimeTest.Negative
    • [       OK ] IsPrimeTest.Negative (0 ms)
    • [ RUN      ] IsPrimeTest.Trivial
    • [       OK ] IsPrimeTest.Trivial (0 ms)
    • [ RUN      ] IsPrimeTest.Positive
    • [       OK ] IsPrimeTest.Positive (0 ms)
    • [----------] 3 tests from IsPrimeTest (6 ms total)
    • [----------] Global test environment tear-down
    • [==========] 6 tests from 2 test cases ran. (18 ms total)
    • [  PASSED  ] 6 tests.
    • C:\Users\zachary>gcc -v
    • Reading specs from C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/specs
    • Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
    • mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
    • -languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --e
    • nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-ja
    • va-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchroniz
    • ation --enable-libstdcxx-debug
    • Thread model: win32
    • gcc version 3.4.2 (mingw-special)
  • 以后在Dev C++中使用gtest,就是注意两点:
    1、需要加入gtest的include目录
    2、加入你需要的那个.a库文件
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP