最好使用GLOB指定源文件,还是在CMake中分别指定每个文件? [英] Is it better to specify source files with GLOB or each file individually in CMake?

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

问题描述

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

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.

首选哪种方式? globbing似乎很容易,但是我听说它有一些缺点。

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.

原始答案:

滚球的优点是:


  • 添加新文件很容易,因为它们仅将
    列在一个位置:
    磁盘上。不通配会造成
    重复。

  • 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

请确保仅保留您想要的文件被添加。乱搞可能会拾起您不想要的杂散
文件。

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)命令来清理文件列表:

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.

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

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