CMake + 谷歌测试 [英] CMake + GoogleTest

查看:24
本文介绍了CMake + 谷歌测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了 googletest,使用 CMake 生成了它的 makefile 并构建了它.现在,我需要在我的测试项目中使用它.

I just downloaded googletest, generated its makefile with CMake and built it. Now, I need to use it in my testing project.

使用 CMake,我被告知不要直接指向 gtest 库(使用 include _directorieslink_directories),而是使用 find_package().

With CMake, I have been advised not pointing to gtest libraries directly (using include _directories or link_directories) but use find_package() instead.

问题是,生成的 gtest 生成文件没有安装目标.我无法理解 find_package(GTest REQUIRED) 如何在没有某种安装的情况下工作.此外,将 gtest 文件夹作为我项目中的子文件夹是不可能的.

The problem is, there is no install target for the gtest makefile generated. I cannot understand how find_package(GTest REQUIRED) could work without some kind of installation. Also, putting the gtest folder as a subfolder in my project is not possible.

感谢您的帮助.

推荐答案

这是一个不寻常的案例;大多数项目指定安装规则.

This is an unusual case; most projects specify install rules.

CMake 的 ExternalProject_Add 模块可能是完成这项工作的最佳工具.这允许您从项目中下载、配置和构建 gtest,然后链接到 gtest 库.

CMake's ExternalProject_Add module is maybe the best tool for this job. This allows you to download, configure and build gtest from within your project, and then link to the gtest libraries.

我已经在使用 Visual Studio 10 和 11 的 Windows 以及使用 GCC 4.8 和 Clang 3.2 的 Ubuntu 上测试了以下 CMakeLists.txt - 它可能需要针对其他平台/编译器进行调整:

I've tested the following CMakeLists.txt on Windows with Visual Studio 10 and 11, and on Ubuntu using GCC 4.8 and Clang 3.2 - it might need adjusted for other platforms/compilers:

cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
project(Test)

# Create main.cpp which uses gtest
file(WRITE src/main.cpp "#include "gtest/gtest.h"

")
file(APPEND src/main.cpp "TEST(A, B) { SUCCEED(); }
")
file(APPEND src/main.cpp "int main(int argc, char **argv) {
")
file(APPEND src/main.cpp "  testing::InitGoogleTest(&argc, argv);
")
file(APPEND src/main.cpp "  return RUN_ALL_TESTS();
")
file(APPEND src/main.cpp "}
")

# Create patch file for gtest with MSVC 2012
if(MSVC_VERSION EQUAL 1700)
  file(WRITE gtest.patch "Index: cmake/internal_utils.cmake
")
  file(APPEND gtest.patch "===================================================================
")
  file(APPEND gtest.patch "--- cmake/internal_utils.cmake   (revision 660)
")
  file(APPEND gtest.patch "+++ cmake/internal_utils.cmake   (working copy)
")
  file(APPEND gtest.patch "@@ -66,6 +66,9 @@
")
  file(APPEND gtest.patch "       # Resolved overload was found by argument-dependent lookup.
")
  file(APPEND gtest.patch "       set(cxx_base_flags "${cxx_base_flags} -wd4675")
")
  file(APPEND gtest.patch "     endif()
")
  file(APPEND gtest.patch "+    if (MSVC_VERSION EQUAL 1700)
")
  file(APPEND gtest.patch "+      set(cxx_base_flags "${cxx_base_flags} -D_VARIADIC_MAX=10")
")
  file(APPEND gtest.patch "+    endif ()
")
  file(APPEND gtest.patch "     set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
")
  file(APPEND gtest.patch "     set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
")
  file(APPEND gtest.patch "     set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
")
else()
  file(WRITE gtest.patch "")
endif()

# Enable ExternalProject CMake module
include(ExternalProject)

# Set the build type if it isn't already
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

# Set default ExternalProject root directory
set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/ThirdParty)

# Add gtest
ExternalProject_Add(
    googletest
    SVN_REPOSITORY http://googletest.googlecode.com/svn/trunk/
    SVN_REVISION -r 660
    TIMEOUT 10
    PATCH_COMMAND svn patch ${CMAKE_SOURCE_DIR}/gtest.patch ${CMAKE_BINARY_DIR}/ThirdParty/src/googletest
    # Force separate output paths for debug and release builds to allow easy
    # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
    CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
               -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
               -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
               -Dgtest_force_shared_crt=ON
    # Disable install step
    INSTALL_COMMAND ""
    # Wrap download, configure and build steps in a script to log output
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON)

# Specify include dir
ExternalProject_Get_Property(googletest source_dir)
include_directories(${source_dir}/include)

# Add compiler flag for MSVC 2012
if(MSVC_VERSION EQUAL 1700)
  add_definitions(-D_VARIADIC_MAX=10)
endif()

# Add test executable target
add_executable(MainTest ${PROJECT_SOURCE_DIR}/src/main.cpp)

# Create dependency of MainTest on googletest
add_dependencies(MainTest googletest)

# Specify MainTest's link libraries
ExternalProject_Get_Property(googletest binary_dir)
if(MSVC)
  set(Suffix ".lib")
else()
  set(Suffix ".a")
  set(Pthread "-pthread")
endif()
target_link_libraries(
    MainTest
    debug ${binary_dir}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${Suffix}
    optimized ${binary_dir}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${Suffix}
    ${Pthread})

如果您在一个空目录(例如 MyTest)中将其创建为 CMakeLists.txt,则:

If you create this as CMakeLists.txt in an empty directory (say MyTest), then:

cd MyTest
mkdir build
cd build
cmake ..

这应该在 MyTest/src 中创建一个基本的 main.cpp 并创建一个项目文件(MyTest/build/Test.sln 在 Windows 上)

This should create a basic main.cpp in MyTest/src and create a project file (MyTest/build/Test.sln on Windows)

当您构建项目时,它应该将 gtest 源下载到 MyTest/build/ThirdParty/src/googletest,并在 MyTest/build/ThirdParty/src/googletest- 中构建它们构建.然后,您应该能够成功运行 MainTest 目标.

When you build the project, it should download the gtest sources to MyTest/build/ThirdParty/src/googletest, and build them in MyTest/build/ThirdParty/src/googletest-build. You should then be able to run the MainTest target successfully.

这篇关于CMake + 谷歌测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆