我正在构建一个中等大小的C ++库,我的CMakeLists.txt
文件开始变得有点长。我正在阅读Robert Martin的书 The Clean Coder ,其中为了代码维护和可读性,他讨论了清晰编码风格的元素。
所以我想看看我是否可以将我的CMakeLists.txt文件分解为一组较小的文件 - 当我运行构建命令时它会集成。然后我就可以管理这些较小的文件,就像我将类分成不同的文件一样。现在作为一个警告,我不是为库的不同元素构建单独的可执行文件。我只是构建一个单独的库,其中包含一组我需要的其他项目的类和函数。
这是我当前的CMakeLists.txt文件。然后我将展示我想如何分解它。
cmake_minimum_required(VERSION 3.7)
project(tvastrCpp)
set (tvastrCpp_VERSION_MAJOR 0)
set (tvastrCpp_VERSION_MINOR 1)
# Find CGAL
find_package(CGAL REQUIRED COMPONENTS Core) # If the dependency is required, use REQUIRED option - if it's not found CMake will issue an error
include( ${CGAL_USE_FILE} )
include(ExternalProject)
set(CMAKE_CXX_STANDARD 14)
find_package(Eigen3 3.1.0)
if (EIGEN3_FOUND)
include( ${EIGEN3_USE_FILE} )
endif()
find_package(Boost 1.58.0 REQUIRED COMPONENTS system filesystem program_options chrono timer date_time REQUIRED)
if(NOT Boost_FOUND)
message(FATAL_ERROR "NOTICE: This demo requires Boost and will not be compiled.")
endif()
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ExternalProject_Add(
catch
PREFIX ${CMAKE_BINARY_DIR}/catch
GIT_REPOSITORY https://github.com/philsquared/Catch.git
TIMEOUT 10
UPDATE_COMMAND ${GIT_EXECUTABLE} pull
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
)
ExternalProject_Get_Property(catch source_dir)
set(CATCH_INCLUDE_DIR ${source_dir}/single_include CACHE INTERNAL "Path to include folder for Catch")
INCLUDE_DIRECTORIES ( "${EIGEN3_INCLUDE_DIR}" )
file(GLOB lib_SRC RELATIVE "lib" "*.h" "*.cpp")
file(GLOB test_SRC RELATIVE "tests" "*.h" "*.cpp")
# need to fix the instruction below to reference library
set(SOURCE_FILES ${lib_SRC})
add_library(tvastrCpp SHARED ${SOURCE_FILES})
add_executable(${PROJECT_NAME} main.cpp random_mat_vector_generator.h random_mat_vector_generator.cpp)
add_executable(my_tests testcases.cpp RipsComplex.h RipsComplex.cpp random_mat_vector_generator.h random_mat_vector_generator.cpp)
add_executable(gd_validator gudhi_validator.cpp)
TARGET_LINK_LIBRARIES( gd_validator ${Boost_LIBRARIES} )
enable_testing(true)
add_test(NAME cooltests COMMAND my_tests)
现在我想为基本设置片创建一个单独的文件:
settings.txt:
cmake_minimum_required(VERSION 3.7)
project(tvastrCpp)
set (tvastrCpp_VERSION_MAJOR 0)
set (tvastrCpp_VERSION_MINOR 1)
然后找到CGAL库的另一篇文章:
find_cgal.txt:
find_package(CGAL REQUIRED COMPONENTS Core) # If the dependency is required, use REQUIRED option - if it's not found CMake will issue an error
include( ${CGAL_USE_FILE} )
include(ExternalProject)
set(CMAKE_CXX_STANDARD 14)
find_package(Eigen3 3.1.0)
if (EIGEN3_FOUND)
include( ${EIGEN3_USE_FILE} )
endif()
等等。
答案 0 :(得分:2)
将您的CMakeLists.txt拆分为:
项目的首要文件:
可重复使用的功能文件:
源文件:
<强>试验:强>
我有一个包含一个CMakeLists.txt的小项目和一个包含多达10个文件的单个库的大型项目。