我如何构建一个C ++项目与多个相互依赖的子目录? [英] How can I build a C++ project with multiple interdependent subdirectories?

查看:149
本文介绍了我如何构建一个C ++项目与多个相互依赖的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++项目,我使用目录作为更多的组织元素 - 可能使用Java中的包或PHP中的目录。目录不是为了自给自足的元素,而只是一种组织整个项目的方式,并使我不被来源所淹没。如何构建我的CMakeLists.txt文件来处理这个?制作目录库似乎不适合这里,因为它们都是相互依存的,并且不打算以这种方式使用。

I have a C++ project where I've used directories as more of an organizational element -- the way one might use packages in Java or directories in PHP. Directories are not intended to be self-sufficient elements, but rather just a way of organizing the whole of the project and keeping me from being overwhelmed by sources. How can I construct my CMakeLists.txt files to deal with this? Making the directories libraries doesn't seem to fit here, since they are all interdependent and not intended to be used that way.

作为一个相关的问题,大多数例子我已经看到了CMake中的多个子目录(并没有很多那些)忽略或掩盖了设置 include_directories 的问题,这是我一直有麻烦。缺少梳理我的源文件以确定哪个文件取决于在哪个目录中,还有只是将 / src / 下的所有目录设置为潜在的包含目录,并让CMake制定哪些是实际上依赖?

As a related issue, most of the examples I've seen of multiple subdirectories in CMake (and there aren't very many of those) have ignored or glossed over the issue of setting include_directories, which is something I've been having trouble with. Short of combing my source files to determine which file depends on which and in what directory, is there anyway to just set all directories under /src/ as potential include directories and let CMake work out which ones are actually dependent?

以下是一个结构示例:

--src
  --top1
    --mid1
      --bot1
        --src1.cpp
        --hdr1.h
      --bot2
        --src2.cpp
        --hdr2.h
    --mid2
      --bot3
        --src3.cpp
        --src4.cpp
        --hdr3.h
  --top2
    --mid3
      --src5.cpp
      --hdr4.h

等等。如何构造 CMakeLists.txt 文件来处理这种结构?

So on and so forth. How can I structure my CMakeLists.txt files to handle this sort of structure?

推荐答案

由于您的项目中的目录结构只是为了保持文件的有序化,一种方法是有一个 CMakeLists.txt 自动查找所有源文件 src 目录,并将所有目录添加为包含头文件的include目录。以下CMake文件可以作为起点:

Since the directory structure in your project is just there to keep your files organized, one approach is to have a CMakeLists.txt that automatically finds all sources files in the src directory and also adds all directories as include directories that have a header file in them. The following CMake file may serve as a starting point:

project (Foo)

file(GLOB_RECURSE Foo_SOURCES "src/*.cpp")
file(GLOB_RECURSE Foo_HEADERS "src/*.h")

set (Foo_INCLUDE_DIRS "")
foreach (_headerFile ${Foo_HEADERS})
    get_filename_component(_dir ${_headerFile} PATH)
    list (APPEND Foo_INCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES Foo_INCLUDE_DIRS)

include_directories(${Foo_INCLUDE_DIRS})
add_executable (FooExe ${Foo_SOURCES})

两个文件(GLOB_RECURSE ... 命令确定源文件和头文件的集合。 foreach 包括所有头文件列表中的目录。

The two file(GLOB_RECURSE ... commands determine the set of source and header files. The foreach loop computes the set of include directories from the list of all header files.

计算源文件集的一个缺点是,当新文件添加到源代码树时,CMake不会自动检测。然后手动重新创建你的构建文件。

One drawback with computing the set of source files is that CMake will not automatically detect when new files are added to your source tree. You manually have to re-create your build files then.

这篇关于我如何构建一个C ++项目与多个相互依赖的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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