将重复的CmakeLists.txt功能移动到公用文件

时间:2018-06-21 13:16:02

标签: cmake

我有多个项目,它们各自的CMakeLists.txt中具有相同/重复的代码。至少我需要一个位置来存储可以放入CMakeLists.txt文件的字符串定义。最好的方法是什么?我的目录结构

common_defs/
        src_files
        hdr_files
        CMakeLists.Txt  - file that has common defs
independent_dir1/
        src_files
        hdr_files
        CMakeLists.Txt  --> Imports/Includes from ../common_defs/CMakeLists.txt
independent_dir2/
        src_files
        hdr_files
        CMakeLists.Txt  --> Imports/Includes from ../common_defs/CMakeLists.txt

independent_dir1和Independent_dir2将彼此独立地构建。在common_defs中构建不应触发在Independent_dirs中构建

1 个答案:

答案 0 :(得分:1)

common_defs/common_defs.cmake中使用通用字符串和定义创建文件,然后将其添加到CMakeLists.txt:

// add path common_defs to include search path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../common_defs")
// include module from file common.cmake searched in search path
include(common_defs)