gmake 从多个目录获取目标文件列表
我不太了解 makefile 的内容,我一直在根据需要学习一些内容。
我的 makefile 最大的失败是我一直手动列出所有文件,尽管这并不是我当前项目变得难以处理的问题。我有 4 个目录,每个目录都有源文件。
如何获取所有目标文件列表而无需手动列出它们。
这不起作用,但它显示了我一直在尝试做的事情。
VPATH = Lib GameCode Moot/Moot Moot/Impl
OBJS = $(subst .cpp, .o, $(VPATH))
foobar: $(OBJS)
g++ -o $@ $^
%.o: %.cpp
g++ -c $< -o $@ -I Moot
clean:
rm main.o lib.o foo.o foobar
I dont know much makefile stuff I've been tending to learn bits as required.
The biggest failing of my makefiles is that I have been listing all the files manually, while this hasn't been a problem my current project is getting unwieldy. I have 4 directories each with sources files.
How can I get all the object file listing without having to list them manually.
This doesn't work, but it shows what I've been trying to do.
VPATH = Lib GameCode Moot/Moot Moot/Impl
OBJS = $(subst .cpp, .o, $(VPATH))
foobar: $(OBJS)
g++ -o $@ $^
%.o: %.cpp
g++ -c lt; -o $@ -I Moot
clean:
rm main.o lib.o foo.o foobar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我手动列出所有文件从来没有遇到过任何问题。与添加有用内容填充文件相比,将文件列出到 makefile 所花费的时间可以忽略不计。
要从不同目录获取所有文件,可能建议使用
通配符
函数。因此my_sources:=$(wildcard *.cpp dir1/*.cpp)
将使变量包含与通配符表达式匹配的源文件。然而,我发现它不如通过 shell 使用通常的 Linux
find
命令方便:Find 比 Make 的内置
wildcard
更强大。您可能还想使用其他 shell 功能(例如管道)来过滤find
的输出(如果 Make 的filter-out
功能不够)。或者类似的东西,以避免过多的变量:你说吧!
Personally, I never had any problem in listing all files manually. Listing a file to the makefile takes negligible time compared to adding filling it with useful content.
To get all files from different directories, one might suggest using
wildcard
function. Somy_sources:=$(wildcard *.cpp dir1/*.cpp)
will make the variable contain source files that match wildcard expression.However, I find it less convenient than using usual Linux
find
command via shell:Find is more powerful than Make's builtin
wildcard
. You might also want to use other shell capabilities, such as pipelines, for example, to filter output offind
(if Make'sfilter-out
function is not enough). Or something like this, to avoid excessive variables:You name it!
使用 VPATH 或 vpath 无法解决您的问题。它提供了查找文件的搜索路径,但您仍然需要列出文件。如果您只需要编译在这些目录中找到的所有和任何 .c/.cpp 文件,那么这应该可以工作:
不需要 VPATH 信息,可以用 .o 替换 .cpp,就像覆盖隐式规则一样。此外,不能使用 ls 代替 find 来查找且仅在指定目录中查找。
Using VPATH or vpath will not work for your problem.. it provides a search path to find files but you still need to list the files. If you just need to compile all and any .c/.cpp files found in those directories then this should work:
The VPATH info is not needed, the substitution of .o for .cpp can go as can the override of the implicit rule. Additionally, not the use of ls instead of find to look in, and only in, the specfified directory.