简体   繁体   English

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

[英]How to use Implicit rule for Makefile with .cpp files?

There is a Implicit rule in Makefiles for C++ files, that use .C or .cc extension. Makefile中的C ++文件中有一个隐含规则,它使用.C或.cc扩展名。 But I usually have the .cpp file extension for C++ source. 但我通常有C ++源代码的.cpp文件扩展名。

How to use the Implicit rule for Makefile with .cpp files? 如何使用.cpp文件的Makefile的隐式规则?

See Catalogue of Implicit Rules : 参见隐式规则目录

Compiling C++ programs 编译C ++程序

no is made automatically from n.cc, n.cpp, or nC with a recipe of the form '$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c'. no是从n.cc,n.cpp或nC自动生成的,配方格式为'$(CXX)$(CPPFLAGS)$(CXXFLAGS)-c'。 We encourage you to use the suffix '.cc' for C++ source files instead of '.C'. 我们建议您对C ++源文件使用后缀“.cc”而不是“.C”。

This is from GNU make docs . 这是来自GNU make docs

'%' 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 no, with another file nc as its prerequisite, provided that nc exists or can be made. 指定如何将文件设为no,并将另一个文件nc作为其先决条件,前提是存在或可以创建nc。

So you could try something similar to 所以你可以尝试类似的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM