gmake 从多个目录获取目标文件列表

发布于 2024-10-02 22:55:18 字数 404 浏览 3 评论 0原文

我不太了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-10-09 22:55:18

就我个人而言,我手动列出所有文件从来没有遇到过任何问题。与添加有用内容填充文件相比,将文件列出到 makefile 所花费的时间可以忽略不计。

要从不同目录获取所有文件,可能建议使用 通配符函数。因此 my_sources:=$(wildcard *.cpp dir1/*.cpp) 将使变量包含与通配符表达式匹配的源文件。

然而,我发现它不如通过 shell 使用通常的 Linux find 命令方便:

# Find all sources
my_sources:=$(shell find -iname '*.cpp')
# Make targets out of them
OBJS=$(my_sources:%.cpp=%.o)

Find 比 Make 的内置 wildcard 更强大。您可能还想使用其他 shell 功能(例如管道)来过滤 find 的输出(如果 Make 的 filter-out 功能不够)。或者类似的东西,以避免过多的变量:

OBJS:=$(shell find -iname '*.cpp' | sed 's/\.cpp$/.o/')

你说吧!

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. So my_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 all sources
my_sources:=$(shell find -iname '*.cpp')
# Make targets out of them
OBJS=$(my_sources:%.cpp=%.o)

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 of find (if Make's filter-out function is not enough). Or something like this, to avoid excessive variables:

OBJS:=$(shell find -iname '*.cpp' | sed 's/\.cpp$/.o/')

You name it!

流云如水 2024-10-09 22:55:18

使用 VPATH 或 vpath 无法解决您的问题。它提供了查找文件的搜索路径,但您仍然需要列出文件。如果您只需要编译在这些目录中找到的所有和任何 .c/.cpp 文件,那么这应该可以工作:

foobar: $(shell ls Lib/*.cpp) $(shell ls GameCode/*.cpp) $(shell ls Moot/Moot/*.cpp)  $(shell ls Moot/Impl/*cpp)
    g++ -o $@ $^

clean:
    rm foobar $(shell ls Lib/*.o) $(shell ls GameCode/*.o) $(shell ls Moot/Moot/*.o)  $(shell ls Moot/Impl/*o)

不需要 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:

foobar: $(shell ls Lib/*.cpp) $(shell ls GameCode/*.cpp) $(shell ls Moot/Moot/*.cpp)  $(shell ls Moot/Impl/*cpp)
    g++ -o $@ $^

clean:
    rm foobar $(shell ls Lib/*.o) $(shell ls GameCode/*.o) $(shell ls Moot/Moot/*.o)  $(shell ls Moot/Impl/*o)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文