我可以从一组较小的文件中构建CMakeLists.txt(以提高CMakeLists.txt的可读性和可维护性) [英] can I build CMakeLists.txt from a set of smaller files (to improve the readability and maintainability of CMakeLists.txt)

查看:132
本文介绍了我可以从一组较小的文件中构建CMakeLists.txt(以提高CMakeLists.txt的可读性和可维护性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个中等大小的C ++库,而我的 CMakeLists.txt 文件开始变得有点长。我正在阅读罗伯特·马丁(Robert Martin)的书《清洁编码器》(The Clean Coder ),其中他讨论了清洁编码风格的要素,以便维护代码并提高可读性。

I am building a medium sized C++ library, and my CMakeLists.txt file is starting to get a bit long. I was reading Robert Martin's book The Clean Coder in which he discusses the elements of clean coding style for the sake of code maintenance and readability.

因此,我想看看是否可以将CMakeLists.txt文件分解为一组较小的文件,这些文件在运行构建命令时会整合在一起。然后,我可以只管理这些较小的文件,就像将类分解为单独的文件一样。请注意,我没有为库的不同元素构建单独的可执行文件。我只是在构建具有其他项目所需的一组类和函数的单个库。

So I wanted to see if I could break up my CMakeLists.txt file into a set of smaller files--that would integrate when I ran a build command. I could then just manage these smaller files, just as I would break up classes into separate files. Now as a caveat, I am not building separate executables for different elements of the library. I am just building a single library with a set of classes and functions that I need for other projects.

这是我当前的CMakeLists.txt文件。然后,我将展示如何分解它。

Here is my current CMakeLists.txt file. Then I will show how I would like to break it up.

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)

现在,我想为基本设置创建一个单独的文件:

Now I would like to create a separate file for the basic settings piece:

 settings.txt:
cmake_minimum_required(VERSION 3.7)
project(tvastrCpp)
set (tvastrCpp_VERSION_MAJOR 0)
set (tvastrCpp_VERSION_MINOR 1)

然后另找一块CGAL库:

Then a separate piece for the finding the CGAL library:

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拆分为:

Split your CMakeLists.txt into:

项目的顶级文件:


  • 包含项目的所有常用设置

  • 包括外部项目

  • 包括子目录

  • 设置常用的编译器设置

  • 构建目标
  • >
  • Contains all common settings of the project
  • Includes the external projects
  • Include the sub directories
  • Set the common compiler settings
  • Builds the targets

可重复使用的功能文件:


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

源文件:


  • 在源代码的每个子目录中放置CMakeList.txt

  • 添加此处将其来源变量SOURCE并使用父范围

测试


  • 使用额外的CMakeLists.txt构建单元测试

  • 在ca中重用CMake文件的先前结构大型测试环境本身

我有一个带有一个CMakeLists.txt的小型项目,一个带有最多10个文件的单个库的大型项目。

I have small projects with one CMakeLists.txt and large projects for a single library with up to 10 files.

这篇关于我可以从一组较小的文件中构建CMakeLists.txt(以提高CMakeLists.txt的可读性和可维护性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆