问题

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的更多相关文章

  1. VS项目中使用Nuget还原包后编译生产还一直报错?

    Nuget官网下载Nuget项目包的命令地址:https://www.nuget.org/packages 今天就遇到一个比较奇葩的问题,折腾了很久终于搞定了: 问题是这样的:我的解决方案原本是好好的 ...

  2. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  3. iOS 之项目中遇到的问题总结

    昨天去一家公司面试,面试官问了我在项目开发中遇到过哪些问题,是什么引起的,怎样解决的? 当时由于有点小紧张只说出了一两点,现在就来好好总结一下. 问题: 1.两表联动 所谓的两表联动就是有左右两个表格 ...

  4. My97DatePicker时间控件在项目中的应用

    一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...

  5. 在项目中同时使用Objective-C和Swift

    苹果发布的Swift语言可以和之前的Objective-C语言同时存在于一个项目中. 可能有人会认为是同一个类文件中既可以有Objective-C也可以有Swift,这是不对的.同一个类文件或同一个代 ...

  6. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  7. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

  8. Web API项目中使用Area对业务进行分类管理

    在之前开发的很多Web API项目中,为了方便以及快速开发,往往把整个Web API的控制器放在基目录的Controllers目录中,但随着业务越来越复杂,这样Controllers目录中的文件就增加 ...

  9. 如何决解项目中hibernate中多对多关系中对象转换json死循环

    先写一下原因吧!我是写的SSH项目,在项目中我遇到的问题是把分页对象(也就是pageBean对象)转化为json数据,下面为代码: public class PageBean <T>{// ...

随机推荐

  1. LeetCode _ Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. [置顶] Android系统移植与调试之------->如何修改Android设备添加3G上网功能

    1.首先先来看一下修改前后的效果对比图 step1.插上3G设备前 step2.插上3G设备后,获取信号中.... step3.插上3G设备后,获取到信号 step4.使用3G信号浏览网页 2.下面讲 ...

  3. HDOJ 1236 排名(练耐心题)

    Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通 ...

  4. class$1,class$2,class$innerclass中的$的含义

    class文件名中的$的含义如下:   $后面的类是$前面的类的内部类 内部类有以下两种情况: 1.普通的组合类形式,即在一个类内部定义一个普通的类  public class Outer { cla ...

  5. python部落刷题宝学到的内置函数(二)

    感觉到刷题宝有一个好处,也许也不是好处,它的答案必须是真正输出的值,也就是说应该输出字符串aaaa的时候,答案必须写成界面上返回的值,即'aaaa'.有利于真正记忆返回值类型,但是....太繁琐了 1 ...

  6. JS(五)

    感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...

  7. linux权限及ntfs文件系统权限的知识

    关于ntfs权限的问题 文件的权限: [-dcbps][u:rwx][g:rwx][a:rwx] 当中: r=4, w=2, x=1,  u=owner, g=group, a=all user   ...

  8. JAVA程序猿怎么才干高速查找到学习资料?

    JAVA程序猿怎么才干高速查找到学习资料? JAVA学习资料在互联网上较为零散,并且大多是英文的.以下介绍3种方式,让程序猿能够高速地找到自己想要的资料. 一.导航站点: 有非常多类似hao123的站 ...

  9. Linux下一些基本操作

    一.忘记root密码 1. sudo passwd root 2. 输入新密码. 二.查看内核版本: 1.查看内核版本命令:1) cat /proc/version 2) uname -a 3)  u ...

  10. Java基础知识强化41:StringBuffer类之StringBuffer的反转功能

    1. StringBuffer 的反转功能: public StringBuffer reverse(): 2. 案例演示: package cn.itcast_05; /* * StringBuff ...