问题描述
我在尝试使用 Google Test 时遇到了一个问题.
I faced with a problem when I was trying to use Google Test.
关于如何使用ExternalProject_Add
将gtest添加到项目中有很多手册,但其中大部分描述了一种基于使用gtest下载zip存档并构建它的方法.
There are lot of manuals on how to use ExternalProject_Add
for the adding gtest into the project, however most of these describe a method based on downloading zip archive with gtest and build it.
众所周知,gtest 是 github 托管和基于 cmake 的项目.所以我想找到原生的 cmake 方式.
As we know gtest is github-hosted and cmake-based project. So I'd like to find native cmake way.
如果这是一个只有头文件的项目,我会这样写:
If this would be a header-only project, I'd write something like:
cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)
find_package(Git REQUIRED)
ExternalProject_Add(
gtest
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/ext
GIT_REPOSITORY https://github.com/google/googletest.git
TIMEOUT 10
UPDATE_COMMAND ${GIT_EXECUTABLE} pull
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
)
ExternalProject_Get_Property(gtest source_dir)
set(GTEST_INCLUDE_DIR ${source_dir}/googletest/include CACHE INTERNAL "Path to include folder for GTest")
set(GTEST_ROOT_DIR ${source_dir}/googletest CACHE INTERNAL "Path to source folder for GTest")
include_directories(${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR} ${GTEST_ROOT_DIR})
message(${GTEST_INCLUDE_DIR})
并从我的 cmake 项目中添加此脚本,例如:
and add this script from my cmake project like:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake.modules/")
include(AddGTest)
....
add_dependencies(${PROJECT_NAME} gtest)
然而,这需要一个构建步骤.
However this requires a build step.
这应该如何实施?
- 通过将
BUILD_COMMAND
添加到ExternaProject_Add
并与生成的库链接? - 或者将 gtest 的 cmakelists 包含到我的项目中,如下所示:
- By adding
BUILD_COMMAND
intoExternaProject_Add
and linking with produced libs? - Or by including gtest's cmakelists into my project, something like this:
add_subdirectory (${CMAKE_SOURCE_DIR}extsrcgtestgoogletestCMakeLists.txt)
这不是正确的方法,因为在项目加载时文件夹不存在.
this is not correct way because on the moment of the project load the folder does not exist.
- 还有其他方法吗?
什么是正确/首选的方式?
What is a correct/prefer way?
推荐答案
我会采用第一种方法.您不需要指定构建命令,因为默认使用 cmake.这可能看起来像:
I would go with the first approach. You don't need to specify a build command because cmake is used by default. This could look like:
cmake_minimum_required(VERSION 3.0)
project(GTestProject)
include(ExternalProject)
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
)
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
add_executable(FirstTest main.cpp)
add_dependencies(FirstTest googletest)
target_link_libraries(FirstTest gtest gtest_main pthread)
我不知道这是否是正确/首选的方式,如果有的话.如果您想实现第二种方法,则必须首先使用 execute_process 下载代码.
I don't know if this is the correct/preferred way if there even is one. If you wanted to implement your second approach you would have to download the code with execute_process first.
这篇关于如何克隆外部(来自 git)cmake 项目并将其集成到本地项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!