本文首发于个人博客https://kezunlin.me/post/4a1427cf/,欢迎阅读!
compile gtest on ubuntu 16.04
Guide
apt-get
sudo apt-get install libgtest-dev
cd /usr/src/gtest # only source files
compile
git clone https://github.com/google/googletest.git
wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz
cd googletest
mkdir build
cd build
cmake-gui ..
make -j8
sudo make install
options:
BUILD_SHARED_LIBS=ON
install to /usr/local/
- for static library, we use
libgtest.a
- for shared library, use use
ligbtest.so
.
gtest-config.cmake
# - Try to find GTEST
#
# The following variables are optionally searched for defaults
# GTEST_ROOT_DIR: Base directory where all GTEST components are found
#
# The following are set after configuration is done:
# GTEST_FOUND
# GTEST_INCLUDE_DIRS
# GTEST_LIBRARIES
# GTEST_LIBRARYRARY_DIRS
include(FindPackageHandleStandardArgs)
set(GTEST_ROOT_DIR "" CACHE PATH "Folder contains Google gtest")
if(WIN32)
find_path(GTEST_INCLUDE_DIR gtest/gtest.h
PATHS ${GTEST_ROOT_DIR})
else()
find_path(GTEST_INCLUDE_DIR gtest/gtest.h
PATHS ${GTEST_ROOT_DIR})
endif()
if(MSVC)
find_library(GTEST_LIBRARY_RELEASE gtest
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES Release)
find_library(GTEST_MAIN_LIBRARY_RELEASE gtest_main
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES Release)
find_library(GTEST_LIBRARY_DEBUG gtest
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES Debug)
find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES Debug)
set(GTEST_LIBRARY optimized ${GTEST_LIBRARY_RELEASE} ${GTEST_MAIN_LIBRARY_RELEASE} debug ${GTEST_LIBRARY_DEBUG} ${GTEST_MAIN_LIBRARY_DEBUG})
else()
find_library(GTEST_LIBRARY gtest
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES lib lib64)
find_library(GTEST_MAIN_LIBRARY gtest_main
PATHS ${GTEST_ROOT_DIR}
PATH_SUFFIXES lib lib64)
endif()
find_package_handle_standard_args(GTEST DEFAULT_MSG GTEST_INCLUDE_DIR GTEST_LIBRARY GTEST_MAIN_LIBRARY)
if(GTEST_FOUND)
set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR})
set(GTEST_LIBRARIES ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
message(STATUS "Found gtest (include: ${GTEST_INCLUDE_DIRS}, library: ${GTEST_LIBRARIES})")
mark_as_advanced(GTEST_ROOT_DIR GTEST_LIBRARY_RELEASE GTEST_LIBRARY_DEBUG
GTEST_MAIN_LIBRARY_RELEASE GTEST_MAIN_LIBRARY_DEBUG
GTEST_LIBRARY GTEST_MAIN_LIBRARY GTEST_INCLUDE_DIR)
endif()
Example
code
whattotest.cpp
#include <math.h>
double squareRoot(const double a) {
double b = sqrt(a);
if(b != b) { // nan check
return -1.0;
}else{
return sqrt(a);
}
}
tests.cpp
#include "whattotest.cpp"
#include <gtest/gtest.h>
TEST(SquareRootTest, PositiveNos) {
ASSERT_EQ(6, squareRoot(36.0));
ASSERT_EQ(18.0, squareRoot(324.0));
ASSERT_EQ(25.4, squareRoot(645.16));
ASSERT_EQ(0, squareRoot(0.0));
}
TEST(SquareRootTest, NegativeNos) {
ASSERT_EQ(-1.0, squareRoot(-15.0));
ASSERT_EQ(-1.0, squareRoot(-0.2));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
if(MSVC)
SET(GTEST_ROOT "C:/Program Files/gtest")
else()
# BOOST_THREAD_LIBRARY /usr/lib/x86_64-linux-gnu/libpthread.so
MESSAGE( [Main] " BOOST_THREAD_LIBRARY = ${BOOST_THREAD_LIBRARY}")
endif(MSVC)
find_package(GTest REQUIRED) # GTest 1.8.0
include_directories(${GTEST_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${BOOST_THREAD_LIBRARY})
#target_link_libraries(runTests gtest pthread)
compile
mkdir build && cd build && cmake-gui ..
make -j8
run
./runTests
link (optional)
use -lgtest
as linker flag and optionally, if you did not write your own test mainroutine, the explicit -lgtest_main
flag.
gtest use pthread, so we need -lpthread
as well.
-lgtest -lgtest_main -lpthread
Reference
- getting-started-with-google-test-on-ubuntu
- googletest repo
- gtest-demo
- setup-googletest-as-a-shared-library-on-linux
- FindGTest
History
- 20180118: created.
- 20180122: add shared library part.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/4a1427cf/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。