CMake项目中集成gtest

使用cmake的ExternalProject_Add相关命令,实现源码获取、编译、集成到本地项目中的动态集成,不需要先安装gtest,而是直接将gtest编译到当前目录中,从而解决了项目部署时gtest的依赖问题。
做法参考:
https://github.com/google/googletest/blob/master/googletest/README.md
Googletest的repo上有较为详细的介绍,这里简单梳理下如何加入到自己的项目中。

先列出我的项目目录结构:

.
├── test
│   ├── CMakeLists.txt
│   ├── CMakeLists.txt.in
│   └── unittest
│       ├── CMakeLists.txt
│       └── test.cpp
└── thirdparty
    └── gtest
        └── googletest-release-1.8.0.zip
  1. test项目目录下,copy下边的内容,并另存为CMakeLists.txt.in
cmake_minimum_required(VERSION 3.1.0)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  URL               ${CMAKE_SOURCE_DIR}/thirdparty/gtest/googletest-release-1.8.0.zip
#  URL               https://github.com/google/googletest/archive/release-1.8.0.zip
  URL_MD5           adfafc8512ab65fd3cf7955ef0100ff5
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

这里说明下,下载的目录可以是网络链接,也可以是本地的一个文件地址,这里为了减少网络下载的负担(实际上是为了防止被墙),使用本地的文件。
Googletest推荐是直接从git上clone,没必要,repo很大的,不如直接下载指定release的源码。

  1. test项目目录下,copy下边的内容,另存为CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
project(test)

configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unittest)
  1. 在unittest目录下,copy下边的内容,另存为CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
project(unittest)

# Now simply link against gtest or gtest_main as needed. 
add_executable(utest test.cpp)
target_link_libraries(utest gtest_main)
enable_testing()
  1. 在unittest目录下,copy 下边的内容,另存为test.cpp
#include "gtest/gtest.h"
TEST(TestSuiteName, TestName) {
    EXPECT_EQ(1, 1);
}

至此,配置完成,可以在unittest目录下依赖gtest实现自己的测试用例了。
如何编译呢?

mkdir build && cd build
cmake .. 
make

你可能感兴趣的:(CMake项目中集成gtest)