4

There is a Implicit rule in Makefiles for C++ files, that use .C or .cc extension. But I usually have the .cpp file extension for C++ source.

How to use the Implicit rule for Makefile with .cpp files?

11
  • .cpp should work fine for C++. .C is usually for C code. Commented Jan 28, 2013 at 19:24
  • 1
    Did you find the documentation of implicit rules? What have you tried and why did it not work? Commented Jan 28, 2013 at 19:26
  • 2
    @juanchopanza No. .C is for C++ too. .c is for C. Commented Jan 28, 2013 at 19:27
  • @H2CO3 OK, my bad. But .cpp should work for C++ out of the box. Commented Jan 28, 2013 at 19:29
  • 1
    @Zhen Hope you are not using 30-year old C++ documentation. Commented Jan 30, 2013 at 7:22

2 Answers 2

4

See Catalogue of Implicit Rules:

Compiling C++ programs

n.o is made automatically from n.cc, n.cpp, or n.C with a recipe of the form ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’. We encourage you to use the suffix ‘.cc’ for C++ source files instead of ‘.C’.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I see the problem, I looked on an old doc page. Now .cpp is also used.
As of today some of the first google results still point to old doc pages.
0

This is from 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 n.o, with another file n.c as its prerequisite, provided that n.c exists or can be made.

So you could try something similar to

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

2 Comments

Ok, I see the problem, I looked on an old doc page. Now .cpp is also used.
Not sure why it refers to some possibly outdated copy of the original GNU make documentation. If you just google for "gnu make manual" the original up-to-date documentation comes up as the first result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.