使用 CMake 自动将文件夹中的所有文件添加到目标? [英] Automatically add all files in a folder to a target using CMake?

查看:30
本文介绍了使用 CMake 自动将文件夹中的所有文件添加到目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将跨平台项目从 Visual C++、XCode 和 makefile 中的单独构建管理系统切换到 CMake.

I am considering switching a cross platform project from separate build management systems in Visual C++, XCode and makefiles to CMake.

我需要的一个基本功能是自动将目录中的所有文件添加到目标.虽然这很容易用 make 做到,但用 Visual C++ 和 XCode 却不容易做到(如果我错了,请纠正我).是否可以直接在 CMake 中完成?怎么样?

One essential feature I need is to add automatically all files in a directory to a target. While this is easy to do with make, it is not easily doable with Visual C++ and XCode (correct me if I am wrong). Is it possible to do it in directly in CMake? How?

推荐答案

从 CMake 3.1+ 开始,开发人员强烈反对 用户使用 file(GLOB文件(GLOB_RECURSE 收集源文件列表.

As of CMake 3.1+ the developers strongly discourage users from using file(GLOB or file(GLOB_RECURSE to collect lists of source files.

注意:我们不建议使用 GLOB 从源树中收集源文件列表.如果在添加或删除源时没有 CMakeLists.txt 文件更改,则生成的构建系统无法知道何时要求 CMake 重新生成.CONFIGURE_DEPENDS 标志可能无法在所有生成器上可靠地工作,或者如果将来添加了不能支持它的新生成器,使用它的项目将被卡住.即使 CONFIGURE_DEPENDS 可靠地工作,在每次重建时执行检查仍然存在成本.

Note: We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate. The CONFIGURE_DEPENDS flag may not work reliably on all generators, or if a new generator is added in the future that cannot support it, projects using it will be stuck. Even if CONFIGURE_DEPENDS works reliably, there is still a cost to perform the check on every rebuild.

请参阅此处的文档.

有两个好答案([1][2]) 在这里详细说明手动列出源文件的原因.

There are two goods answers ([1], [2]) here on SO detailing the reasons to manually list source files.

这是可能的.例如.与 file(GLOB:

It is possible. E.g. with file(GLOB:

cmake_minimum_required(VERSION 2.8)

file(GLOB helloworld_SRC
     "*.h"
     "*.cpp"
)

add_executable(helloworld ${helloworld_SRC})

请注意,如果添加或删除源文件,这需要手动重新运行 cmake,因为生成的构建系统不知道何时要求 CMake 重新生成,并且在每次构建时都这样做会增加构建时间.

Note that this requires manual re-running of cmake if a source file is added or removed, since the generated build system does not know when to ask CMake to regenerate, and doing it at every build would increase the build time.

从 CMake 3.12 开始,您可以将 CONFIGURE_DEPENDS 标志传递给 file(GLOB 以在调用构建时自动检查和重置文件列表.您将编写:

As of CMake 3.12, you can pass the CONFIGURE_DEPENDS flag to file(GLOB to automatically check and reset the file lists any time the build is invoked. You would write:

cmake_minimum_required(VERSION 3.12)

file(GLOB helloworld_SRC CONFIGURE_DEPENDS "*.h" "*.cpp")

这至少可以让您避免每次添加文件时手动重新运行 CMake.

This at least lets you avoid manually re-running CMake every time a file is added.

这篇关于使用 CMake 自动将文件夹中的所有文件添加到目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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