如何克隆外部(来自 git)cmake 项目并将其集成到本地项目中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38006584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:04:20  来源:igfitidea点击:

How to clone and integrate external (from git) cmake project into local one

c++gitcmakegoogletest

提问by amigo421

I faced with a problem when I was trying to use Google Test.

我在尝试使用 Google Test 时遇到了一个问题。

There are lot of manuals on how to use ExternalProject_Addfor the adding gtest into the project, however most of these describe a method based on downloading zip archive with gtest and build it.

有很多关于如何使用ExternalProject_Add将 gtest 添加到项目中的手册,但是大多数手册描述了一种基于使用 gtest 下载 zip 存档并构建它的方法。

As we know gtest is github-hosted and cmake-based project. So I'd like to find native cmake way.

我们知道 gtest 是 github 托管和基于 cmake 的项目。所以我想找到原生的 cmake 方式。

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})

and add this script from my cmake project like:

并从我的 cmake 项目中添加此脚本,例如:

 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.

然而,这需要一个构建步骤。

How should this be implemented?

这应该如何实施?

  1. By adding BUILD_COMMANDinto ExternaProject_Addand linking with produced libs?
  2. Or by including gtest's cmakelists into my project, something like this:
  1. 通过添加BUILD_COMMANDExternaProject_Add,并与生产库链接?
  2. 或者通过将 gtest 的 cmakelists 包含到我的项目中,如下所示:

add_subdirectory (${CMAKE_SOURCE_DIR}\ext\src\gtest\googletest\CMakeLists.txt)

add_subdirectory (${CMAKE_SOURCE_DIR}\ext\src\gtest\googletest\CMakeLists.txt)

this is not correct way because on the moment of the project load the folder does not exist.

这不是正确的方法,因为在项目加载时文件夹不存在。

  1. Any other ways?
  1. 还有其他方法吗?

What is a correct/prefer way?

什么是正确/首选的方式?

回答by David Marquant

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。这可能看起来像:

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)

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.

我不知道这是否是正确/首选的方式,如果有的话。如果您想实现第二种方法,则必须首先使用 execute_process 下载代码。