gpt4 book ai didi

c++ - 我可以从一组较小的文件构建 CMakeLists.txt 吗(以提高 CMakeLists.txt 的可读性和可维护性)

转载 作者:行者123 更新时间:2023-11-30 03:32:28 25 4
gpt4 key购买 nike

我正在构建一个中型 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()

等等。

最佳答案

将您的 CMakeLists.txt 拆分为:

项目的顶级文件:

  • 包含项目的所有常用设置
  • 包括外部项目
  • 包含子目录
  • 设置通用编译器设置
  • 建立目标

可重用函数文件:

  • 将您自己的 CMake 宏和函数存储到一个单独的文件中,以便在您的不同项目中重复使用

源文件:

  • 在源代码的每个子目录下放置一个CMakeList.txt
  • 将此处的源添加到变量 SOURCE 并使用 PARENT SCOPE

测试:

  • 使用额外的 CMakeLists.txt 构建单元测试
  • 大型测试环境复用CMake文件原有结构

我有一个包含一个 CMakeLists.txt 的小项目和一个包含最多 10 个文件的库的大型项目。

关于c++ - 我可以从一组较小的文件构建 CMakeLists.txt 吗(以提高 CMakeLists.txt 的可读性和可维护性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com