Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make with dynamic target names

I know that I can use the automatic variable $@ from within a target in a makefile to retrieve the name of the current target. Now I wonder whether it is possible to have a makefile that looks like this:...

$@:
    g++ $@ && ./a.out

The idea is that typing make test.cpp should run g++ test.cpp && ./a.out where as make main.cpp should run g++ main.cpp && ./a.out.

I see that wildcards might be strongly related to my problem. However, a makefile like this:

*.cpp:
    g++ $@ && ./a.out

does indeed create the appropriate targets, but running make test.cpp with such a makefile yields make: >>test.cpp<< is already up-to-date - without the compilation taking place.

like image 246
Max Beikirch Avatar asked Feb 05 '14 18:02

Max Beikirch


2 Answers

The behavior you described is the behavior you want from the make system and this is why it is so loved by many and scales so well.

make test.cpp does not work because what you want is make a.out

Try starting from a simple but correct makefile and evolve it to your needs

To trigger the compilation remove test.o and say make test.o, or say touch test.cpp and then again make test.o. If you do not want intermediate object files (I tell you - you do want them) you can apply the above advice replacing test.o with a.out.

You are trying to push where you should be pulling :-)

As @MadScientist reminded in his comment, one of useful features of GNU Make is a vast set of default rules which may save quite a bit of boilerplate when you don't need fine control of each step.

like image 54
bobah Avatar answered Nov 15 '22 09:11

bobah


I had a similar need when trying to make Go program. I solved my problem by setting the target to *.

In addition, you need to pass the .PHONY directive to filter out the files you want to process, otherwise something like make: 'clock1' is up to date, because my makefile already has clock1's folder in its directory.

GO_BUILD = go build
GO_BIN ?= $(shell go env GOPATH)/bin
PROG = $(GO_BIN)/$@

*:
    $(GO_BUILD) -o $(PROG) ./$@/*.go
    ls -al $(PROG)
test:
    @echo ${GO_BIN}

.PHONY: clock1 clock2 countdown*

RUN make command:

$ make clock1   
go build -o /data/go/bin/clock1 ./clock1/*.go
ls -al /data/go/bin/clock1
-rwxr-xr-x  1 Terry  staff  3013840 Sep 25 16:53 /data/go/bin/clock1

$ make clock2 
go build -o /data/go/bin/clock2 ./clock2/*.go
ls -al /data/go/bin/clock2
-rwxr-xr-x  1 Terry  staff  3177296 Sep 25 16:53 /data/go/bin/clock2

$ make countdown1
go build -o /data/go/bin/countdown1 ./countdown1/*.go
ls -al /data/go/bin/countdown1

like image 33
lupguo Avatar answered Nov 15 '22 09:11

lupguo