How to change make builtin rules

0

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.

Esteban Crespi

Posted 2017-10-03T12:45:22.147

Reputation: 153

Answers

1

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.

Daniel B

Posted 2017-10-03T12:45:22.147

Reputation: 40 502

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 – 2017-10-03T14:16:36.967

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 – 2017-10-03T14:18:04.697