如何在项目中使用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>{// ...
随机推荐
- delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)
uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...
- POJ 2923 Relocation
题目大意:有n个物品,有两辆车载重分别是c1,c2.问需要多少趟能把物品运完. (1 ≤ Ci ≤ 100,1 ≤ n ≤ 10,1 ≤ wi ≤ 100). 题解:n小思状压.我们先把所有一次可以拉 ...
- Android写入文件操作权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses- ...
- Redis哨兵模式
Redis-Sentinel redis的哨兵模式 Redis Sentinel 模式简介 Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案 ...
- js让菜单栏一直悬浮在顶部,经典代码
js让菜单栏一直悬浮在顶部,经典代码 很简单,你只需要把下面代码放到js中:$(function(){ //获取要定位元素距离浏览器顶部的距离 var na ...
- 怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障
怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障 英文原文引自 http://xphelpandsupport.mvps.org/how_do_i_repa ...
- [ES6] Class Inherit
In constructor, you can call parent's constuctor() method by supert(); class ShoppingCart { construc ...
- WSAWaitForMultipleEvents()
简述:只要指定事件对象中的一个或全部处于有信号状态,或者超时间隔到,则返回. #include <winsock2.h> DWORD WSAAPI WSAWaitForMultipleEv ...
- Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
1. StringBuffer的替换功能: public StringBuffer replace(int start, int end, String str): 2. 案例演示: p ...
- redis安装过程中遇到的问题
正常的 wget http://download.redis.io/releases/redis-3.0.7.tar.gz下载 解压缩 tar -zxvf redis-3.0.7.tar.gz cd ...