我面临一些与如何处理C++项目有关的问题。我有一组所有使用git跟踪的存储库(托管在gitlab上)。
repo_A
:我当前的项目repo_B
:我创建的一个库,需要在repo_A
中使用在
repo_B
中包括repo_A
的最佳实践是什么? 。repo_B
吗?然后通过使用CMakelists repo_B
中的repo_A
repo_B
上添加repo_A
作为子模块cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
我们是用户级别的原始安装库。但是,在开始使用Jenkins之后,我们面临一些问题。
最佳答案
最好的选择是混合版本。我通常将外部库作为git子模块(在PROJECT_ROOT /子模块中)进行跟踪,并为所有这些库创建通用的CMake文件,如下所示:
find_package(GTest)
if(GTEST_FOUND)
message(STATUS "googletest was found!")
else()
message(STATUS "googletest was not found, local build is needed!")
include(ExternalProject)
ExternalProject_Add(googletest-project
URL ${CMAKE_SOURCE_DIR}/submodules/googletest/
PREFIX "${CMAKE_CURRENT_BINARY_DIR}/ext/googletest"
CMAKE_ARGS -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_GTEST=ON
DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
)
ExternalProject_Get_Property(googletest-project install_dir)
ExternalProject_Get_Property(googletest-project source_dir)
file(MAKE_DIRECTORY ${install_dir}/include)
add_library(gtest STATIC IMPORTED)
add_library(gtest_main STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION
${install_dir}/lib/libgtest.a)
set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION
${install_dir}/lib/libgtest_main.a)
set_property(TARGET gtest APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${install_dir}/include)
set_property(TARGET gtest_main APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${install_dir}/include)
add_dependencies(gtest googletest-project)
add_dependencies(gtest_main googletest-project)
set(GTEST_LIBRARIES gtest)
set(GTEST_MAIN_LIBRARIES gtest_main)
set(GTEST_INCLUDE_DIRS ${install_dir}/include)
endif()
这样,当您已经在系统上安装googletest时(例如,在预安装了所有外部依赖项的Docker镜像中),将不会构建googletest。如您所见,我从子模块目录构建googletest,而不会从github克隆。