一、安装Gtest
1、下载最新版gtest源码
git clone https://github.com/google/googletest
cd googletest/googletest
2、编辑CMakeLists.txt文件,将option(BUILD_SHARED_LIBS “Build shared libraries (DLLs).” OFF)中的OFF改为ON
3、编译(需要安装cmake)
cmake CMakeLists.txt
make
我使用的是Ubuntu16.04,这里遇到了错误“this file requires compiler and library support for the iso c++ 2011 standard”。解决方法:在CMakeLists.txt中添加一句
SET( CMAKE_CXX_FLAGS "-std=c++11 -O3")
并且在之后编译gtest文件的时候采用如下方式编译,问题解决
g++ -std=c++11 your_file.cpp -o main
4、make成功后生成了两个静态库文件,libgtest_main.so和libgtest.so,将它们拷贝到/usr/local/lib/目录下,并执行ldconfig
sudo cp lib/libgtest*.so /usr/lib
sudo cp –a include/gtest /usr/include
sudo ldconfig
5、安装完成,编写一个测试文件并编译运行,运行成功
g++ -std=c++11 test.cpp -lgtest -lpthread
./a.out
二、Gtest在ros中的使用
可参考https://leooo48.github.io/2018/08/14/gtest/
注意在工程中使用时需要在cmakelist中加上gtest库文件,方法可以参考另一篇博文:https://blog..net/BockSong/article/details/93642300