在CMake中指定源文件的最佳方法 [英] Best way to specify sourcefiles in CMake

查看:1177
本文介绍了在CMake中指定源文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cmake中,有几种方法可以指定目标的源文件。
一个是使用globbing(文档),for示例:

  FILE(GLOB dir / *)

另一个是单独指定每个文件,我想还有更多的方法来做到这一点。



是最好的?最好在,有更多的优点而不利。



Globbing似乎很容易,但我听说它有一些缺点。我不记得哪一个。

解决方案

完全公开:我最喜欢简单的globbing方法,但多年来已经认识到,对于大型多开发人员项目,明确列出文件不太容易出错。



原始答案: >




globbing的优点是:




  • 添加新文件很容易,因为
    只列在一个地方:
    磁盘。


  • 您的CMakeLists.txt档案会缩短
    。这是一个大的加号,如果你
    有很多文件。不是全局化
    会导致在大量文件列表中丢失CMake逻辑




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




  • CMake将正确跟踪磁盘上新文件的依赖性 - 如果我们使用
    glob then文件不是第一次运行的时候运行CMake不会得到
    选择


  • 。 Globbing可能会收到你不想要的stray
    文件。




问题,您可以简单地触摸执行glob的CMakeLists.txt,通过使用touch命令或不更改写入文件。这将强制cmake重新运行并获取新文件。



要解决第二个问题,您可以仔细地将代码组织到目录中,这是你可能做的无论如何。在最坏的情况下,可以使用列表(REMOVE_ITEM)命令清除文件的全局列表:

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

这可能会咬你的情况是,如果你正在使用像 git-bisect 在同一个build目录下尝试旧版本的代码。在这种情况下,您可能需要清理和编译超过必要的,以确保您在列表中获得正确的文件。这是一个这样的情况,你已经在你的脚趾,这不是一个真正的问题。


In Cmake, there are several ways to specify the sourcefiles for a target. One is to use globbing (documentation), for example:

FILE (GLOB dir/*)

Another one is to specify each file individually, and I guess there are even more ways to do this.

Which way is the best? Best as in, has more advantages than disadvantages.

Globbing seems easy, but I heard it has some downsides. I can't remember which.

解决方案

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.

Original answer:


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.

  • 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 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.

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.

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})

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中指定源文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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