如何在Makefile中使用隐式规则处理.cpp文件?

4

为C++文件在Makefile中有一个隐式规则,可以使用.C或.cc扩展名。但我通常使用.cpp扩展名作为C++源文件。

如何在Makefile中使用.cpp文件的隐式规则?


1
你找到隐式规则的文档了吗?你尝试过什么,为什么不起作用? - David Grayson
2
@juanchopanza 不是的,.C 也适用于 C++。而 .c 则是用于 C 语言。 - user529758
@juanchopanza 是的,没错。 - user529758
@H2CO3:在大小写不敏感的文件系统上,使用“.C”作为C++源文件是有问题的。 - Keith Thompson
1
@Zhen 希望你没有使用30年前的C++文档。 - Maxim Egorushkin
显示剩余6条评论
2个回答

4

请参阅隐式规则目录

C++程序编译

n.cc、n.cpp或n.C可以自动地生成n.o,其配方为‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’。我们建议您使用后缀‘.cc’来命名C++源文件,而不是‘.C’。


1
好的,我明白问题了,我看了一个旧的文档页面。现在也使用.cpp。 - Zhen
截至今天,一些谷歌搜索结果仍然指向旧的文档页面。 - arthropod

0

这里来自于GNU make文档

‘%’ in a prerequisite of a pattern rule stands for the same stem that was matched by the ‘%’ in the target. In order for the pattern rule to apply, its target pattern must match the file name under consideration and all of its prerequisites (after pattern substitution) must name files that exist or can be made. These files become prerequisites of the target. Thus, a rule of the form

 %.o : %.c ; recipe...

specifies how to make a file n.o, with another file n.c as its prerequisite, provided that n.c exists or can be made.

所以你可以尝试类似的东西

%.o : %.cpp %.hpp
   $(CC) $(CFLAGS) $@

好的,我明白问题了,我看了一个旧的文档页面。现在也使用.cpp。 - Zhen
不确定为什么它会引用一些可能过时的GNU make文档副本。如果您只是在谷歌上搜索“gnu make手册”,那么最新的原始文档将作为第一个结果显示。 - Maxim Egorushkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接