为C++文件在Makefile中有一个隐式规则,可以使用.C或.cc扩展名。但我通常使用.cpp扩展名作为C++源文件。
如何在Makefile中使用.cpp文件的隐式规则?
请参阅隐式规则目录:
C++程序编译
n.cc、n.cpp或n.C可以自动地生成n.o,其配方为‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’。我们建议您使用后缀‘.cc’来命名C++源文件,而不是‘.C’。
这里来自于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) $@
.C
也适用于 C++。而.c
则是用于 C 语言。 - user529758