0%

compile gtest on ubuntu 16.04

Series

Guide

apt-get

1
2
sudo apt-get install libgtest-dev
cd /usr/src/gtest # only source files

compile

1
2
3
4
5
6
7
8
9
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

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# - 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

1
2
3
4
5
6
7
8
9
10
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

1
2
mkdir build && cd build && cmake-gui ..
make -j8

run

1
./runTests

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.

1
-lgtest -lgtest_main -lpthread

Reference

History

  • 20180118: created.
  • 20180122: add shared library part.