您的位置:首页 > 其它

gtest 编译、安装和使用

2016-06-29 15:32 375 查看
本例以 gtest-1.7.0 为例进行讲解。

一、要求:

1. 安装 cmake

二、编译

1. 下载 gtest 源码包,并解压,如:/home/hdc/gtest-1.7.0;
2.  编译 gtest 动态库,进入 gtest-1.7.0 目录,编辑 CMakeLists.txt,修改如下几行:
     10 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
     19 option(gtest_build_tests "Build all of gtest's own tests." OFF)
     21 option(gtest_build_samples "Build gtest's sample programs." OFF)
     将 OFF 修改为 ON,保存并退出。
3.  创建 build 目录,并进入该目录;
4. cmake ..
5. make
6. ls *.so
     libgtest_dll.so                libgtest_main_no_rtti.so  libgtest_main_use_own_tuple.so  libgtest.so

     libgtest_main_no_exception.so  libgtest_main.so          libgtest_no_exception.so
7. sudo cp *.so /usr/lib/gtest
8. cd ../include
9. sudo cp -pr gtest /usr/include
10. sudo chown root:root /usr/include/gtest -R
11. sudo chmod u+w /usr/include/gtest -R
12. sudo mkdir /usr/lib/gtest && cd /usr/lib/gtest
13. sudo ln -s libgtest.so libgtest.so.6
14. sudo ln -s libgtest.so libgtest.so.6.0
15. sudo ln -s libgtest_main.so libgtest_main.so.6
16. sudo ln -s libgtest_main.so libgtest_main.so.6.0

三、使用

1. vim func.h

[cpp] view
plain copy

 





#ifndef FUNC_C  

#define FUNC_C  

  

  

#ifdef __cplusplus  

extern "C"  

{  

#endif  

  

  

int fac(int nInput);  

  

  

#ifdef __cplusplus  

}  

#endif  

  

  

#endif // FUNC_C  

2. vim func.c

[cpp] view
plain copy

 





#include "func.h"  

  

  

int fac(int nInput)    

{    

    if(nInput < 0) {   

      return -1;  

    }  

  

    int nRev = 1;    

    for(int i = 1; i <= nInput; ++i){    

      nRev *= i;    

    }    

    return nRev;    

}  

3. vim fac_test.cpp

[cpp] view
plain copy

 





#include <limits>  

#include "func.h"  

#include "gtest/gtest.h"  

    

TEST(Fac_test, input_negative){    

    EXPECT_EQ(-1, fac(-1));    

    EXPECT_EQ(-1, fac(-2));    

    EXPECT_EQ(-1, fac(-5));    

}    

    

TEST(Fac_test, input_zero){    

    EXPECT_EQ(1, fac(0));    

}    

    

TEST(Fac_test, input_positive){    

    EXPECT_EQ(1, fac(1));    

    EXPECT_EQ(2, fac(2));    

    EXPECT_EQ(6, fac(3));    

}  

4. vim Makefile

[plain] view
plain copy

 





CC = gcc  

CPP = g++  

LINK = g++  

CFLAGS = -g -Wall -Werror -Wextra -std=gnu99  

CPPFLAGS = -g -Wall -Werror -Wextra  

LIBS = -L/usr/lib/gtest -lgtest -lgtest_main  

  

  

C__SOURCES = $(wildcard *.c)  

CPPSOURCES = $(wildcard *.cpp)  

OBJECTS = $(patsubst %.c, %.o, $(C__SOURCES)) $(patsubst %.cpp, %.o, $(CPPSOURCES))  

TARGET = sample_unittest  

  

  

first: all  

  

  

%.o: %.c  

    $(CC) -c $(CFLAGS) -o $@ $<  

  

  

%.o: %.cpp  

    $(CPP) -c $(CPPFLAGS) -o $@ $<  

  

  

all: $(TARGET)  

  

  

$(TARGET): $(OBJECTS)  

    $(LINK) $(CPPFLAGS) $(LIBS) -o $(TARGET) $(OBJECTS)  

  

  

.PHONY: clean  

  

  

clean:  

    rm -f $(TARGET) $(OBJECTS)  

5. make
6. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/gtest
7. ./sample_unittest



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: