Googletest build

Git repository

User guide

Build Instructions

사실 위 링크에 잘 정리되어 있어서 위 내용만 보셔도 충분합니다. 다만 위의 Build instruction을 따라하려면 root 권한으로 install 해야하고, 설치 위치가 /usr/local/ 로 설정되어있는 것이 마음에 들지 않아 저는 조금 다른 방법으로 사용했습니다.

Build

Build Instructions과는 다르게 설치 위치를 다른 곳으로 바꾸었습니다. 예제에서는 ~/gtest에 설치했습니다.

$git clone https://github.com/google/googletest.git -b release-1.11.0
$cd googletest        # Main directory of the cloned repository.
$mkdir build          # Create a directory to hold the build output.
$cd build
$cmake -DCMAKE_INSTALL_PREFIX:PATH=~/gtest ..
$make
$make install

이제 ~/gtest 디렉토리에 include, lib 디렉토리가 생겼습니다.

사용 예제

https://github.com/google/googletest/tree/master/googletest/samples google test sample 주소입니다. sample.cc, sample.h, sample 1_unittest.cc을 사용해보겠습니다.

# Current working directory is googletest/build
$cd ../..
$mkdir googletest_example && cd googletest_example
$cp ../googletest/googletest/samples/sample1.h .
$cp ../googletest/googletest/samples/sample1.cc .
$cp ../googletest/googletest/samples/sample1_unittest.cc .

main.cc를 작성합니다.

$vi main.cc
// main.cc
#include "gtest/gtest.h"

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

CMakeLists.txt를 작성합니다.

$vi CMakeLists.txt
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

#set the project name
project(gtest_example)
set(CMAKE_CXX_STANDARD 17)
add_executable(main main.cc sample1.cc sample1_unittest.cc)
target_include_directories(main PUBLIC ~/gtest/include)
target_link_directories(main PUBLIC ~/gtest/lib)
target_link_libraries(main gtest gtest_main gmock pthread)

빌드를 하고 실행을 해봅니다.

$mkdir build && cd build
$cmake ..
$make
$./main

Categories:

Updated:

Leave a comment