Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile with multiple targets

Hopefully this is a very simple question. I have a makefile pattern rule that looks like this:

%.so : %.f %.pyf
    f2py -c -L${LAPACK_DIR} ${GRASPLIBS} -m $* $^ ${SOURCES} --opt='-02' --f77flags='-fcray-pointer' >> silent.txt

I want the makefile to build a number of .so files, so I tried to get it to build two files (radgrd_py.so and lodiso_py.so) by doing this:

radgrd_py.so lodiso_py.so:

%.so : %.f %.pyf
f2py -c -L${LAPACK_DIR} ${GRASPLIBS} -m $* $^ ${SOURCES} --opt='-02' --f77flags='-fcray-pointer' >> silent.txt

and then tried this:

radgrd_py.so:

lodiso_py.so:

%.so : %.f %.pyf
f2py -c -L${LAPACK_DIR} ${GRASPLIBS} -m $* $^ ${SOURCES} --opt='-02' --f77flags='-fcray-pointer' >> silent.txt

But in each case, it only builds the first target that I specify. If I run 'make radgrd_py.so' it works fine, I'm just not sure how to specify a list of files that need to be built so I can just run 'make'.

like image 572
Kazza789 Avatar asked Aug 19 '10 07:08

Kazza789


People also ask

What is $@ in makefile?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

What are targets in makefile?

A simple makefile consists of "rules" with the following shape: target ... : dependencies ... command ... ... A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.

What does .phony do in makefile?

The special rule . PHONY is used to specify that the target is not a file. Common uses are clean and all . This way it won't conflict if you have files named clean or all .

How do I call a target in makefile?

When you type make or make [target] , the Make will look through your current directory for a Makefile. This file must be called makefile or Makefile . Make will then look for the corresponding target in the makefile. If you don't provide a target, Make will just run the first target it finds.


2 Answers

The usual trick is to add a 'dummy' target as the first that depends on all targets you want to build when running a plain make:

all: radgrd_py.so lodiso_py.so

It is a convention to call this target 'all' or 'default'. For extra correctness, let make know that this is not a real file by adding this line to your Makefile:

.PHONY: all
like image 55
schot Avatar answered Oct 26 '22 23:10

schot


Best way is to add:

.PHONY: all
.DEFAULT: all
all: radgrd_py.so lodiso_py.so

Explanations:

make uses the first target appearing when no .DEFAULT is specified.

.PHONY informs make that the targets (a coma-separated list, in fact) don't create any file or folder.

all: as proposed by schot

like image 28
levif Avatar answered Oct 26 '22 23:10

levif