GNU make:生成源文件列表
自动生成源文件列表(.c、.cpp 等,而不是头文件)是否正常?
你会怎么做?我正在考虑使用 find 和 sed 。
编辑:该项目是一个图书馆。所有源文件都位于项目目录内。不存在不相关的源文件。所有目标文件都是使用相同的编译器选项生成的。我正在考虑按照 Tromey 的方式生成所有源文件的列表,然后为每个源文件生成一个依赖文件。这是一个可行的方法吗?
更多编辑:它是一个相当大的 C++ 库。该项目正在开发中。最大限度地减少重新编译是非常需要的。
谢谢。
Is it normal to generate the list of source files (.c, .cpp etc, not headers) automatically?
How would you do it? I am thinking to use find and sed.
EDIT: The project is a library. All source files are inside the project directory. No unrelated source files are there. All object files are generated using the same compiler options. I am thinking to generate the list of all source files and then a dependency file for each source file following Tromey's way. It is a viable approach?
MORE EDIT: It is a fairly large C++ library. The project is being developed. Minimising recompilation is highly desired.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 GNU make 中,您可以使用通配符。有关示例,请参阅此问题。
With GNU make you can use wildcards. See this question for an example.
普通的?这很常见,但并不明智。
许多人使用 Make 通配符或
find
或类似的东西来生成某个目录树中存在的所有源文件的列表,然后将它们提供给编译器并将对象链接在一起。这是一个脆弱的解决方案,会给您带来麻烦。如果源文件之间出现冲突(例如void foo()
的两个单独定义),链接器会抱怨,并且如何解决问题可能并不明显。您可能会发现自己有一大堆源文件,其中许多对您的项目来说是不必要的,这会减慢您的构建速度并导致冲突。如果您想在另一个可执行文件中使用其中一些源(但不是全部),则必须诉诸符号链接或其他一些手段。更好的方法是在 makefile 中指定给定目标所需的对象,然后让 Make 确定要使用哪些源。这正是Make所擅长的。没有可靠的方法来自动维护对象列表,您只需要手动完成即可,但这并不需要太多工作;如果你经常更换它们,以至于这确实是一件苦差事,那么你就做错了。
编辑:
如果该项目是您所描述的库,那么是的,这是一种可行的方法,而且是一个非常好的方法。 Tromey 的方法可以很好地防止不必要的重新编译。
Normal? It is common, but not wise.
Many people use Make wildcards or
find
or something similar to generate a list of all the source files that exist in a certain directory tree, then feed them to the compiler and link the objects together. This is a brittle solution that will get you into trouble. If a conflict appears among the source files (e.g. two separate definitions ofvoid foo()
) the linker will complain and it may not be obvious how to fix the problem. You may find yourself with a forest of source files, many of them unnecessary to your project, slowing down your builds and causing conflicts. And if you want to make use of some of these sources (but not all) in another executable, you'll have to resort to symbolic links or some other kludgery.A better approach is to specify in the makefile which objects are necessary to a given target, then let Make figure out which sources to use. This is what Make is good at. There is no reliable way to maintain the object lists automatically, you just have to do it by hand, but it's not that much work; if you're changing them often enough that this is a real chore, then you're doing something wrong.
EDIT:
If the project is a library as you describe, then yes, this is a viable method, and a pretty good one. And Tromey's method will work quite nicely to prevent unnecessary recompilation.