Default Rules in Make

Is there a mechanism in make that allows you to use global implicit rules by default, which are available anywhere, like the built-in rules?

Make provides some built-in rules for compiling C / C ++ / Fortran files, without even requiring a Makefile for simple cases. However, when compiling other languages ​​(for example, Go language programming files), a Makefile is always required. I would like to expand my Makeenvironment so that implicit rules are available by default.

+4
source share
3 answers

This is usually not advisable, as this may result in your Makefile being less portable; it wouldn’t work on someone else’s machine if they hadn’t configured this method.

However, if you want to do this, create a “global” Makefile somewhere with your default rules for Go files, and 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 included its source at the beginning of the file.

+6
source

I assume that you mean the fact that you can do

 make hello.o 

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

The most portable way to do this is the following (in your Makefile):

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

The first line ( .SUFFIXES ) warns that you will treat them as special suffixes; the second line says "here is the recipe for creating .bar from .foo . The third line gives the command for this - $> and $@ get the changed make to the input and output file names.

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

A more flexible method that only works with GNU make is to use its implicit rules . If you can guarantee that you will use GNU make, then this is probably recommended.

+3
source

While I agree with dmazzoni, I will just add my make recipe for Go Makefile :

 # Include default Golang Make magic include $(GOROOT)/src/Make.$(GOARCH) # Hack the following line your_program: your_program.$O $(LD) -o $@ $^ # Compiles .go-files into architecture-specific binaries %.$O: %.go $(GC) -o $@ $^ clean: rm your_program *.$O 

(Note: $O - DOLLAR + UPPERCASE-o - not zero!)

Until I tested it on all the machines available to me, I believe that it should be well tolerated.

+3
source

Source: https://habr.com/ru/post/1312764/


All Articles