1

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

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

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

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

4

1 回答 1

1

tldr:您应该尝试将 google_test 集成为“子项目”而不是预构建并使用“元”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)
于 2018-01-11T11:07:52.143 回答