Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default rules in Make

Is there a mechanism in make to allow for default global implicit rules that are available anywhere, similar to the built-in rules?

Make provides some built-inimplicit rules for compiling C/C++/Fortran files, without even requiring a Makefile for simple cases. However, when compiling other languages (e.g. Go programming language files), a Makefile is always required. I would like to extend my Makeenvironment to have implicit rules available by default.

like image 440
notnoop Avatar asked Dec 12 '22 21:12

notnoop


2 Answers

This is not normally desirable, as it would cause your Makefile to be less portable; it wouldn't work on somebody else's machine if they didn't have it set up that way.

However, if you want to do this, create a "global" Makefile somewhere with your default rules for Go files, then add its path to the MAKEFILES environment variable. This global Makefile will be processed before any Makefile when you run "make", just as if you had included its source at the top of the file.

like image 173
dmazzoni Avatar answered Jan 02 '23 22:01

dmazzoni


I'm assuming you're referring to the fact that you can do

make hello.o

and make will automatically know how to make the .o from a .c file (or indeed from a .f or .p, if one exists) - but you want to do this for custom file types (say, building a .bar from a .foo.

The most portable way of doing this is as follows (in your Makefile):

.SUFFIXES: .foo .bar
.foo.bar:
        foo2bar -in $> -out $@

The first line (.SUFFIXES) warns make that you'll be treating these as special suffixes; the second line says "here's a recipe for making a .bar from a .foo. The third line gives the command for doing this - $> and $@ get changed by make to the input and output filenames.

NOTE: The indent for the third line MUST be a tab character.

A much more flexible method, that only works with GNU make, is to use its support for implicit rules. If you can guarantee you'll be using GNU make then this is probably to be recommended.

like image 24
psmears Avatar answered Jan 02 '23 22:01

psmears