Makefile 中的动态目标
我正在尝试创建一个 Makefile,该文件的每个 src/
子文件夹都有一个目标,以便它创建一个静态库。
我目前正在尝试这个:
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) -c -o $@ $<
lib%.a: $(patsubst %.cpp, %.o, $(wildcard src/%/*.cpp))
$(AR) rcs $@ $^
但这不起作用,目标匹配有效,但依赖项跟踪不起作用。
如果我只保留 src/%/*.cpp
,它会正确完成正确目录中的 .cpp
文件,但当我尝试在字符串函数中使用它时要将 .cpp
转换为 .o
,%
不再起作用。
I'm trying to create a Makefile that has a target per src/
subfolder so that it creates a static lib.
I am currently trying this:
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) -c -o $@ lt;
lib%.a: $(patsubst %.cpp, %.o, $(wildcard src/%/*.cpp))
$(AR) rcs $@ $^
But this doesn't work, the target matching works, but the dependency tracking doesn't.
If I just leave alone src/%/*.cpp
that completes properly to the .cpp
files in the proper dir, but the moment I try to use it inside string functions to convert the .cpp
to .o
the %
does not work anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很棘手,因为据我所知,您不能在先决条件列表中使用像 patsubst 这样的函数。有不止一种方法可以做到这一点;这也许是最不难看的。将路径存储在变量中,然后重新调用 make,以便可以在规则之外构建先决条件列表。
This is tricky because as far as I know you can't use functions like patsubst in the prerequisite list. There is more than one way to do it; this is perhaps the least ugly. Store the path in a variable, then reinvoke make so that you can construct the prerequisite list outside the rule.