Do with dynamic goal names

I know that I can use the automatic variable $@from the target in the makefile to get the name of the current target. Now I am wondering if it is possible to have a make file that looks like this: ...

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

The idea is that input make test.cppshould start g++ test.cpp && ./a.outwhere it make main.cppshould run g++ main.cpp && ./a.out.

I see that wildcards can be strongly related to my problem. However, the make file looks like this:

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

really creates the appropriate goals, but running make test.cppwith such a makefile gives make: >>test.cpp<< is already up-to-date- without compilation.

+3
source share
2 answers

, , - , make, .

make test.cpp , make a.out

, make

, test.o make test.o, touch test.cpp, make test.o. ( - ), , test.o a.out.

, :-)

@MadScientist, GNU Make - , , .

+4

Go. , *.

, .PHONY, , , - make: 'clock1' is up to date, make clock1 .

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*

make:

$ 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

0

All Articles