Sahil Arora
Sahil Arora

Reputation: 885

How to modify implicit GNUMake rules to compile files?

GNUMake has implicit rules to compile certain file types, for instance, in my directory if I have a file 1.cpp, and I write on terminal make 1, the following command gets executed:

g++     1.cpp   -o 1

All this happens without any Makefile in the directory, due to implicit Make rules. However, I am unable to figure out how to modify these rules for my benefit. For instance, if I need to compile my file like this:

g++ -std=c++14 -O2 -g -w -o 1 1.cpp

and for doing this, I want to run the command: make 1, it should do it. Also, it should be generic for any file, for instance I now create a file 2.cpp and write make 2, it should compile it and produce the executable, even if there is no rule for 2 in my Makefile. Also, if I now go to another directory where this explicit rule has not been mentioned, it should compile according to the default implicit rules only. How to I achieve this?

Upvotes: 2

Views: 137

Answers (1)

user657267
user657267

Reputation: 21030

One way is to set the variables used by the implicit rules in your environment

set CXXFLAGS="-std=c++14 -O2 -g -w"

If you only want this to apply to a single directory, then place a Makefile in the directory with the following

CXXFLAGS := -std=c++14 -O2 -g -w

Upvotes: 2

Related Questions