我有一个包含多个c源文件的目录(每个文件本身就是一个小程序),我想一次将其全部编译并在子目录bin /中输出每个二进制文件的二进制文件。二进制文件的名称应为c源文件之一,但不带.c扩展名。如何在Makefile中完成类似的操作?
例:
-src
ll.c
lo.c
-bin
ll
lo
我想到的第一件事是:
CFLAGS=-Wall -g
SRC=$(wildcard *.c)
all: $(SRC)
gcc $(CFLAGS) $(SRC) -o bin/$(SRC)
但这并不能像我想的那样有效。
该行all: $(SRC)
告诉make,all
目标已将每个源文件作为先决条件。
然后,该目标的配方(gcc $(CFLAGS) $(SRC) -o bin/$(SRC)
)尝试在所有源文件上运行gcc,并告诉其创建bin/<first word in
g with the rest of the words from
()的其他参数作为输出的$(SRC)$(SRC)`作为输出。
您想要更多类似这样的东西:
SRCS := $(wildcard *.c)
# This is a substitution reference. http://www.gnu.org/software/make/manual/make.html#Substitution-Refs
BINS := $(SRCS:%.c=bin/%)
CFLAGS=-Wall -g
# Tell make that the all target has every binary as a prequisite and tell make that it will not create an `all` file (see http://www.gnu.org/software/make/manual/make.html#Phony-Targets).
.PHONY: all
all: $(BINS)
bin:
mkdir $@
# Tell make that the binaries in the current directory are intermediate files so it doesn't need to care about them directly (and can delete them). http://www.gnu.org/software/make/manual/make.html#index-_002eINTERMEDIATE
# This keeps make from building the binary in the current directory a second time if you run `make; make`.
.INTERMEDIATE: $(notdir $(BINS))
# Tell make that it should delete targets if their recipes error. http://www.gnu.org/software/make/manual/make.html#index-_002eDELETE_005fON_005fERROR
.DELETE_ON_ERROR:
# This is a static pattern rule to tell make how to handle all the `$(BINS)` files. http://www.gnu.org/software/make/manual/make.html#Static-Pattern
$(BINS) : bin/% : % | bin
mv $^ $@
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句