在 CMake 中使用 GLOB 或每个文件单独指定源文件更好吗? [英] Is it better to specify source files with GLOB or each file individually in CMake?

查看:22
本文介绍了在 CMake 中使用 GLOB 或每个文件单独指定源文件更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake 提供了多种方法来指定目标的源文件.一种是使用通配符(documentation),例如:

CMake offers several ways to specify the source files for a target. One is to use globbing (documentation), for example:

FILE(GLOB MY_SRCS dir/*)

另一种方法是单独指定每个文件.

Another method is to specify each file individually.

哪种方式更受欢迎?通配符似乎很容易,但我听说它有一些缺点.

Which way is preferred? Globbing seems easy, but I heard it has some downsides.

推荐答案

完全公开:我最初更喜欢 globbing 方法,因为它的简单性,但多年来我逐渐认识到,明确列出文件不太容易出错大型、多开发者项目.

Full disclosure: I originally preferred the globbing approach for its simplicity, but over the years I have come to recognise that explicitly listing the files is less error-prone for large, multi-developer projects.

原答案:

globbing 的优点是:

The advantages to globbing are:

  • 添加新文件很容易只列在一处:在盘.不通配创建重复.

  • It's easy to add new files as they are only listed in one place: on disk. Not globbing creates duplication.

您的 CMakeLists.txt 文件将是更短.这是一个很大的优势,如果你有很多文件.不通配导致您丢失 CMake 逻辑在庞大的文件列表中.

Your CMakeLists.txt file will be shorter. This is a big plus if you have lots of files. Not globbing causes you to lose the CMake logic amongst huge lists of files.

使用硬编码文件列表的优点是:

The advantages of using hardcoded file lists are:

  • CMake 将正确跟踪磁盘上新文件的依赖项 - 如果我们使用glob 然后当你运行 CMake 时第一次没有通配的文件将不会得到捡起来

  • CMake will track the dependencies of a new file on disk correctly - if we use glob then files not globbed first time round when you ran CMake will not get picked up

您确保只添加您想要的文件.Globbing 可能会误入歧途您不想要的文件.

You ensure that only files you want are added. Globbing may pick up stray files that you do not want.

为了解决第一个问题,您可以简单地触摸"执行 glob 的 CMakeLists.txt,方法是使用 touch 命令或直接写入文件而不做任何更改.这将强制 CMake 重新运行并选取新文件.

In order to work around the first issue, you can simply "touch" the CMakeLists.txt that does the glob, either by using the touch command or by writing the file with no changes. This will force CMake to re-run and pick up the new file.

要解决第二个问题,您可以将代码小心地组织到目录中,无论如何您都可能会这样做.在最坏的情况下,您可以使用 list(REMOVE_ITEM) 命令来清理文件的 globbed 列表:

To fix the second problem you can organize your code carefully into directories, which is what you probably do anyway. In the worst case, you can use the list(REMOVE_ITEM) command to clean up the globbed list of files:

file(GLOB to_remove file_to_remove.cpp)
list(REMOVE_ITEM list ${to_remove})

唯一可以让您感到困扰的真实情况是,如果您正在使用诸如 git-bisect 在同一构建目录中尝试旧版本的代码.在这种情况下,您可能需要进行不必要的清理和编译,以确保在列表中获得正确的文件.这是一种极端情况,而且您已经处于警觉状态,这并不是真正的问题.

The only real situation where this can bite you is if you are using something like git-bisect to try older versions of your code in the same build directory. In that case, you may have to clean and compile more than necessary to ensure you get the right files in the list. This is such a corner case, and one where you already are on your toes, that it isn't really an issue.

这篇关于在 CMake 中使用 GLOB 或每个文件单独指定源文件更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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