如何在项目中使用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.0 #已经完成编译好的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(3, sum(1,2));
}

/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.0/include -g -Wall -Wextra -c ./sum.c
g++ -I/gtest-1.6.0/include -g -Wall -Wextra -c ./test_sum.c
g++ -I/gtest-1.6.0/include -I/gtest-1.6.0 -g -Wall -Wextra -c \
            /gtest-1.6.0/src/gtest-all.cc
g++ -I/gtest-1.6.0/include -I/gtest-1.6.0 -g -Wall -Wextra -c \
            /gtest-1.6.0/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.0/include -g -Wall -Wextra sum.o test_sum.o gtest_main.a -o test_sum -lpthread

 自己生成lib,用简单的命令行生成测试程序

cd ~/gtest-1.6.0
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.0
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

 

转载于:https://www.cnblogs.com/code-style/p/3374520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值