技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

如何在项目中使用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

 

posted on   codestyle  阅读(737)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2012-10-17 再次改进日志类 --!
点击右上角即可分享
微信分享提示