如何在项目中使用gtest1.6
问题
gtest1.6版本的README里说该版本不支持make install,其意思就是说你没法通过make命令把gtest安装到/usr/local/lib之类的目录,所以你也没办法通过下面的命令来编译测试程序,那么该怎么使用gtest呢?
g++ -lgtest -L /usr/local/lib -I gtest.h test.c
方案
假定已经把源码解压到当前用户的主目录下并且已经成功编译了,在gtest解压目录里有一个make目录(~/gtest-1.6.0/make),里面有一个Makefile文件,这个make脚本会使用同级别的sample目录下(~/gtest-1.6.0/sample)的源码编译一个测试程序,那么我们可以把这个Makefile文件复制到我们的项目目录下,修改gtest路径信息,源码路径信息,以及源码的编译规则,不熟悉的同学需要补一下makefile的知识
GTEST_DIR = /home/lishujun/gtest-1.6. #已经完成编译好的gtest目录的绝对路径,不能使用相对路径 USER_DIR = . # 程序所在目录,写点号即可,上面写死路径是被迫的
如果这个过程你没犯错的话,直接在这个目录下执行make命令就能看见一个亲切的可执行文件啦
补充
不过如果不把gtest放在系统的目录里我总觉得不爽,后来我尝试了一下把gtest放到系统的根目录(/gtest-1.6.0),修改GTEST_DIR发现也是可以的
源码
/home/lishujun/script/test/sum.h
#ifndef _SUM_H_
#define _SUM_H_ int sum(int a, int b); #endif
/home/lishujun/script/test/sum.c
#include "sum.h" int sum(int a, int b)
{
return a + b;
}
/home/lishujun/script/test/test_sum.c
#include <stdio.h>
#include "sum.h"
#include "gtest/gtest.h" TEST(SUM,ADD){
EXPECT_EQ(, sum(,));
}
/home/lishujun/script/test/Makefile
# A sample Makefile for building Google Test and using it in user
# tests. Please tweak it to suit your environment and project. You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
# make [all] - makes everything.
# make TARGET - makes the given target.
# make clean - removes all files generated by make. # Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify. # Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = /gtest-1.6.0 # Where to find user code.
USER_DIR = . # Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include # Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra # All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = test_sum # All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h # House-keeping build targets. all : $(TESTS) clean :
rm -f $(TESTS) gtest.a gtest_main.a *.o # Builds gtest.a and gtest_main.a. # Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS) # For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest-all.cc gtest_main.o : $(GTEST_SRCS_)
$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
$(GTEST_DIR)/src/gtest_main.cc gtest.a : gtest-all.o
$(AR) $(ARFLAGS) $@ $^ gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^ # Builds a sample test. A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function. sum.o : $(USER_DIR)/sum.c $(USER_DIR)/sum.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sum.c test_sum.o : $(USER_DIR)/test_sum.c \
$(USER_DIR)/sum.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/test_sum.c test_sum : sum.o test_sum.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ -lpthread
编译命令
cd ~/script/test
make
./test_sum
Makefile生成的编译命令(ubuntu12)
g++ -I/gtest-1.6./include -g -Wall -Wextra -c ./sum.c
g++ -I/gtest-1.6./include -g -Wall -Wextra -c ./test_sum.c
g++ -I/gtest-1.6./include -I/gtest-1.6. -g -Wall -Wextra -c \
/gtest-1.6./src/gtest-all.cc
g++ -I/gtest-1.6./include -I/gtest-1.6. -g -Wall -Wextra -c \
/gtest-1.6./src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
a - gtest-all.o
a - gtest_main.o
g++ -I/gtest-1.6./include -g -Wall -Wextra sum.o test_sum.o gtest_main.a -o test_sum -lpthread
自己生成lib,用简单的命令行生成测试程序
cd ~/gtest-1.6.
g++ -I./include -I./ -c ./src/gtest_main.cc
g++ -I./include -I./ -c ./src/gtest-all.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
sudo cp gtest_main.a /usr/local/lib/gtest_main.a cd ~/script/test
g++ -c sum.c
g++ -c test_sum.c
g++ sum.o test_sum.o /usr/local/lib/gtest_main.a -lpthread
./a.out
生成libgtest,通过-lgtest进行引用
cd ~/gtest-1.6.
g++ -I./include -I./ -c ./src/gtest_main.cc
g++ -I./include -I./ -c ./src/gtest-all.cc
ar rv libgtest.a gtest-all.o gtest_main.o
sudo cp libgtest.a /usr/local/lib/ cd ~/script/test
g++ -c sum.c
g++ -c test_sum.c
g++ sum.o test_sum.o -lgtest -lpthread
./a.out
测试代码如果分散在多个文件里
g++ test.o sum.o test_sum.o -lgtest -lpthread
注意:
我记得之前如果要把库装到/usr/local/lib下是要刷新系统记录的,我不知道为什么这次没要刷新,如果要刷新,可以查看:
http://www.cnblogs.com/code-style/p/3223381.html
如何在项目中使用gtest1.6的更多相关文章
- VS项目中使用Nuget还原包后编译生产还一直报错?
Nuget官网下载Nuget项目包的命令地址:https://www.nuget.org/packages 今天就遇到一个比较奇葩的问题,折腾了很久终于搞定了: 问题是这样的:我的解决方案原本是好好的 ...
- ABP项目中使用Swagger生成动态WebAPI
本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...
- iOS 之项目中遇到的问题总结
昨天去一家公司面试,面试官问了我在项目开发中遇到过哪些问题,是什么引起的,怎样解决的? 当时由于有点小紧张只说出了一两点,现在就来好好总结一下. 问题: 1.两表联动 所谓的两表联动就是有左右两个表格 ...
- My97DatePicker时间控件在项目中的应用
一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...
- 在项目中同时使用Objective-C和Swift
苹果发布的Swift语言可以和之前的Objective-C语言同时存在于一个项目中. 可能有人会认为是同一个类文件中既可以有Objective-C也可以有Swift,这是不对的.同一个类文件或同一个代 ...
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
- 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入
在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...
- Web API项目中使用Area对业务进行分类管理
在之前开发的很多Web API项目中,为了方便以及快速开发,往往把整个Web API的控制器放在基目录的Controllers目录中,但随着业务越来越复杂,这样Controllers目录中的文件就增加 ...
- 如何决解项目中hibernate中多对多关系中对象转换json死循环
先写一下原因吧!我是写的SSH项目,在项目中我遇到的问题是把分页对象(也就是pageBean对象)转化为json数据,下面为代码: public class PageBean <T>{// ...
随机推荐
- 【HDOJ】4515 小Q系列故事——世界上最遥远的距离
简单题目,先把时间都归到整年,然后再计算.同时为了防止减法出现xx月00日的情况,需要将d先多增加1,再恢复回来. #include <cstdio> #include <cstri ...
- 【HDOJ】1493 QQpet exploratory park
超水的动态规划.最后要对概率求Sigma. #include <cstdio> #include <cstring> #include <cstdlib> #def ...
- 有关autoresizingMask属性遇到的一个小问题
前言:在讲述这个小问题之前,我们有必要先了解一下UIViewAutoresizing的有关属性概念和使用详解. 参考:自动布局之autoresizingMask使用详解(Storyboard& ...
- RequireJS进阶(三)
进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...
- 重写boost内存池
最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小 ...
- asdasd
adasdasd asdasd asdasd asd
- 自定义input file样式
自定义input file样式:一般都是通过隐藏input,通过定义label来实现.这种做法要注意的是label的for属性要指定input对应的id; <!DOCTYPE html> ...
- StoryBoard 设置TabBar SelectImage 和tintColor
如图:StoryBoard 结构是 Tabbar + Navi + ViewController 需求:需要修改TabBar的Image 和SelectImage 设置Image 设置SelectIm ...
- 关于AppStore上传相关问题
1.电脑本地证书CertificateSigningRequest.certSigningRequest一定要一致(包括开发者证书,尤其是发布证书要一致,否则无法正常上传),此类错误Xcode一般会提 ...
- ionic学习教程地址梳理
Ionic是一个新的.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户界面范例,例如像列表(lists).标签 ...