如何将外部(从git)cmake项目克隆并集成到本地项目 [英] How to clone and integrate external (from git) cmake project into local one

查看:600
本文介绍了如何将外部(从git)cmake项目克隆并集成到本地项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用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)

但是这需要构建步骤。

应如何实施?


  1. 通过将 BUILD_COMMAND 添加到 ExternaProject_Add 并链接到生成的库?

  2. 还是通过将gtest的cmakelist包含到我的项目中,就像这样:

  1. By adding BUILD_COMMAND into ExternaProject_Add and linking with produced libs?
  2. Or by including gtest's cmakelists into my project, something like this:

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. 还有其他方法吗?

什么是正确/首选方式?

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项目克隆并集成到本地项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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