使用 gcc -MM 标志在单个文件中生成所有项目依赖项
我想通过 Makefile 使用 gcc -M 标志生成一个包含源文件的所有依赖项的单个依赖项文件。我用谷歌搜索了这个解决方案,但是,提到的所有解决方案都是为多个对象生成多个 deps 文件。
DEPS = make.dep
$(OBJS): $(SOURCES)
@$(CC) -MM $(SOURCEs) > $(DEPS)
@mv -f $(DEPS) $(DEPS).tmp
@sed -e 's|.$@:|$@:|' < $(DEPS).tmp > $(DEPS)
@sed -e 's/.*://' -e 's/\\$$//' < $(DEPS).tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(DEPS)
@rm -f $(DEPS).tmp
但它无法正常工作。请告诉我哪里出错了。
I want to generate a single dependency file which consists of all the dependencies of source files using gcc -M flags through Makefile. I googled for this solution but, all the solutions mentioned are for generating multiple deps files for multiple objects.
DEPS = make.dep
$(OBJS): $(SOURCES)
@$(CC) -MM $(SOURCEs) > $(DEPS)
@mv -f $(DEPS) $(DEPS).tmp
@sed -e 's|.$@:|$@:|' < $(DEPS).tmp > $(DEPS)
@sed -e 's/.*://' -e 's/\\$//' < $(DEPS).tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$/:/' >> $(DEPS)
@rm -f $(DEPS).tmp
But it is not working properly. Please tell me where i'm making the mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
沿着这些思路,我用来将所有依赖项放入单个文件中:
这基本上会导致每当修改项目中的任何 C 或 H 文件时,所有用户(而不是系统)依赖项都会重新构建到单个文件中。
+++++++++ 编辑 +++++++++++++
我已经找到了更好的做事方式。我为每个源文件生成一个单独的 dep 文件。这是基本的 makefile:
它做了两件事:
Something along these lines is what I use to get all my dependencies in a single file:
This basically causes all the user ( as opposed to system ) dependencies to be rebuilt into a single file whenever any C or H file in the project is modified.
+++++++++ EDIT +++++++++++
I've since found a better way of doing things. I generate a separate dep file for each source file. Here is the basic makefile:
This does two things:
我认为这是
gcc -M
的预期行为,通常您会执行以下操作:(...很多目标...)
注意,
-include
不是include
,因为在至少运行一次构建之前,依赖项显然不会存在。无论如何,依赖关系是在每个模块的基础上生成的。另请注意,
gcc -M
并不总是按照您期望的方式工作,大致取决于您使用的 gcc 版本。我认为你想要的是名为 makedep 的东西,它可以在 makefile 中不需要 sed hackery 来完成你想要的事情。
I think is is expected behaviour for
gcc -M
, where typically you'd do something like this:(... lots of targets ...)
Note,
-include
notinclude
as the dependencies will obviously not exist until at least one build has been run. Regardless, dependencies are generated on a per module basis.Also note that
gcc -M
does not always work as you would expect it to work, broadly depending on what version of gcc you happen to be using.I think what you want is something called makedep, which does what you want without sed hackery in the makefile.