I would like to know if there is a way to change the rules applied automatically by make when there is no makefile. To give an example, if you write make foo in a directory containing a file called foo.cpp it executes g++ foo.cpp -o foo even if there is no makefile in the directory.

I would like to change them to suit my needs, say something like g++ foo.cpp -o foo --std=c++11 -W -O2 -I .... but also to add new rules for other extensions such as .c, .java or .tex

I know how to define the new rules, but I don't know where to specify them other than creating a makefile in every directory. I suppose there is some file that holds all these builtin rules but I haven't been able to find it or how to overload it with one of my own.

I'm working mostly with Mingw and cygwin.


According to the manual, the rule is actually:

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’.

So that leaves with either adjusting your environment variables (for example, in ~/.bashrc) or just creating a Makefile which overrides that rule.

Use make -p in a directory with no Makefile to learn all the defaults and built-in rules.

| improve this answer | |
  • Thanks for your answer. The list of rules is very useful as it allows me to tailor its behaviour to my needs. I'm still surprised that there is no way to override these rules or remove them. – Esteban Crespi Oct 3 '17 at 14:16
  • You can override them. Using a Makefile. You can also disable them, by using --no-builtin-rules. It’s all in the manual. – Daniel B Oct 3 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.