在CMake中的多个项目之间共享ExternalProject

时间:2018-01-09 11:55:28

标签: makefile build cmake googletest external-project

我想在不同的CMake项目之间共享一个ExternalProject。想象一下如下结构:

prj1
|- CMakeLists.txt
|- ...
prj2
|- CMakeLists.txt
|- ...
lib
|- ext
 |- gtest
  |- CMakeLists.txt
   |- googletest
    |_ actual_google_test_files

我想要获得的是告诉CMakeLists.txtlib/ext/gtest中的gtest与ExternalProject一起使用,而不是每次为每个项目重新构建gtest。

理想情况下,gtest在其文件夹中构建一次,项目只使用它。我尝试使用像这里解释的ExternalProject(http://kaizou.org/2014/11/gtest-cmake/)并在项目中包含lib/ext/gtest/CMakeLists.txt,但gtest会为每个用户重新编译。

1 个答案:

答案 0 :(得分:1)

tldr:您应该尝试将google_test整合为“子项目”,而不是预先构建并使用“meta”CMakeLists.txt ......

请阅读https://crascit.com/2015/07/25/cmake-gtest/

CMakeLists.txt.in:

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

的CMakeLists.txt:

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )

# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This adds
# the following targets: gtest, gtest_main, gmock
# and gmock_main
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src"
             "${CMAKE_BINARY_DIR}/googletest-build")


add_subdirectory(prj1)
add_subdirectory(prj2)