简体   繁体   English

GNU make:生成源文件列表

[英]GNU make: generate list of source files

Is it normal to generate the list of source files (.c, .cpp etc, not headers) automatically? 生成源文件列表(.c,.cpp等,而不是标题)是否正常?

How would you do it? 你会怎么做? I am thinking to use find and sed. 我想用find和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. 我正在考虑按照Tromey的方式生成所有源文件的列表,然后生成每个源文件的依赖文件。 It is a viable approach? 这是一种可行的方法吗?

MORE EDIT: It is a fairly large C++ library. 更多编辑:这是一个相当大的C ++库。 The project is being developed. 该项目正在制定中。 Minimising recompilation is highly desired. 最大限度地减少重新编译是非常需要的。

Thanks. 谢谢。

With GNU make you can use wildcards. 使用GNU make,您可以使用通配符。 See this question for an example. 请参阅此问题以获取示例。

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. 许多人使用Make通配符或find或类似的东西来生成某个目录树中存在的所有源文件的列表,然后将它们提供给编译器并将对象链接在一起。 This is a brittle solution that will get you into trouble. 这是一个脆弱的解决方案,会让你陷入困境。 If a conflict appears among the source files (eg two separate definitions of void foo() ) the linker will complain and it may not be obvious how to fix the problem. 如果源文件中出现冲突(例如, void foo()两个单独定义),链接器将会抱怨,如何解决问题可能并不明显。 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. 如果你想在另一个可执行文件中使用其中一些来源(但不是全部),你将不得不求助于符号链接或其他一些kludge。

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. 更好的方法是在makefile中指定哪些对象对于给定目标是必需的,然后让Make确定要使用哪些源。 This is what Make is good at. 这就是Make擅长的。 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. Tromey的方法可以很好地防止不必要的重新编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM