有关Makefile的问题-什么是"$ +"? & .c文件/依赖项在哪里叫? [英] Questions about Makefile - what is "$+" & where are .c files/dependencies called here ?
问题描述
我遇到了这个Makefile(通过一个名为sendip的开源项目找到了它)
I came across this Makefile (found it through an open source project called sendip)
我对此文件有两个困惑-
I have two confusions regarding this file -
- 在哪里将
.c
文件指定为依赖项?尽管像ipv6.so
,tcp.so
之类的所有库都可以很好地生成,但是这里的哪一行负责?
- Where are
.c
files being specified as dependencies here? Although all the libraries likeipv6.so
,tcp.so
are being generated fine, but which line here is responsible for it?
我认为这是一行.....对吗?
I think this is the line ..... Right ??
%.so: %.c $(LIBS)
$(CC) -o $@ $(CFLAGS) $(LIBCFLAGS) $+ $(LIBS)
但$(LIBS)
仅指定一些.o文件.这$+
在做什么吗?
but $(LIBS)
only specify some .o files. IS this $+
doing something ?
2.我从没听说过$+
.我试图找出它,并遇到了许多其他内容,例如$?
,$@
,$<
等,但从未见过.我认为它的行为类似于$?
,但仍然需要指定.c
依赖关系.
2.I have never heard of $+
. I tried to find it out and came across many others like $?
, $@
, $<
, etc. but never seen this one. I think it behaves like $?
but still it also demands .c
depndencies to be specified.
制作文件:
#configureable stuff
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man/man1
LIBDIR ?= $(PREFIX)/lib/sendip
#For most systems, this works
INSTALL ?= install
#For Solaris, you may need
#INSTALL=/usr/ucb/install
CFLAGS= -fPIC -fsigned-char -pipe -Wall -Wpointer-arith -Wwrite-strings \
-Wstrict-prototypes -Wnested-externs -Winline -Werror -g -Wcast-align \
-DSENDIP_LIBS=\"$(LIBDIR)\"
#-Wcast-align causes problems on solaris, but not serious ones
LDFLAGS= -g -rdynamic -lm -ldl
#LDFLAGS_SOLARIS= -g -lsocket -lnsl -lm
LDFLAGS_SOLARIS= -g -lsocket -lnsl -lm -ldl
LDFLAGS_LINUX= -g -rdynamic -lm -ldl
LIBCFLAGS= -shared
CC= gcc-4.4
PROGS= sendip
BASEPROTOS= ipv4.so ipv6.so
IPPROTOS= tcp.so udp.so icmp.so
UDPPROTOS= rip.so ripng.so ntp.so
TCPPROTOS= bgp.so
PROTOS= $(BASEPROTOS) $(IPPROTOS) $(UDPPROTOS) $(TCPPROTOS)
LIBS= libsendipaux.a
LIBOBJS= csum.o compact.o protoname.o headers.o parseargs.o
cryptomod.o crc32.o
SUBDIRS= mec
all: $(LIBS) subdirs sendip $(PROTOS) sendip.1 sendip.spec
#there has to be a nice way to do this
sendip: sendip.o gnugetopt.o gnugetopt1.o compact.o
sh -c "if [ `uname` = Linux ] ; then \
$(CC) -o $@ $(LDFLAGS_LINUX) $(CFLAGS) $+ ; \
elif [ `uname` = SunOS ] ; then \
$(CC) -o $@ $(LDFLAGS_SOLARIS) $(CFLAGS) $+ ;\
else \
$(CC) -o $@ $(LDFLAGS) $(CFLAGS) $+ ; \
fi"
libsendipaux.a: $(LIBOBJS)
ar vr $@ $?
subdirs:
for subdir in $(SUBDIRS) ; do \
cd $$subdir ;\
make ;\
cd .. ;\
done
protoname.o: mec/protoname.c
$(CC) -o $@ -c -I. $(CFLAGS) $+
headers.o: mec/headers.c
$(CC) -o $@ -c -I. $(CFLAGS) $+
parseargs.o: mec/parseargs.c
$(CC) -o $@ -c -I. $(CFLAGS) $+
cryptomod.o: mec/cryptomod.c
$(CC) -o $@ -c -I. $(CFLAGS) $+
crc32.o: mec/crc32table.h mec/crc32.c
$(CC) -o $@ -c -I. $(CFLAGS) mec/crc32.c
mec/crc32table.h: mec/gen_crc32table
mec/gen_crc32table > mec/crc32table.h
sendip.1: ./help2man $(PROGS) $(PROTOS) subdirs VERSION
./help2man -n "Send arbitrary IP packets" -N >sendip.1
推荐答案
是的,您是正确的.
目标定义以%
字符开头时,它定义的是目标模式,而不是特定模式.因此,%.so
表示一个目标,用于生成其他目标所需或用户所需的所有.so文件. %.c
也是一种模式,表示所有.c
文件.
When a target definition start with the %
character it defines a target pattern, not a specific pattern. So %.so
means a target to generate all .so files needed by other targets or required by the user. The %.c
also is a pattern and means all .c
files.
因此,$(CC) -o $@ $(CFLAGS) $(LIBCFLAGS) $+ $(LIBS)
表示命令输出将具有正在生成的目标的名称($ @->与该模式匹配的目标的名称)...,而$+
表示所有与之匹配的文件prerequisite
模式(即%.c
).
So the $(CC) -o $@ $(CFLAGS) $(LIBCFLAGS) $+ $(LIBS)
means the command output will have the name of the target being generated ($@ -> name of the target that matched the pattern) ... and the $+
means all the files that matched with the prerequisite
pattern (i.e: %.c
).
看看GNU make手册,特别是规则目录,了解$+
,$^
,...的含义.
Take a look at the GNU make manual, specifically at the Catalogue of Rules to see what $+
, $^
, ... mean.
这篇关于有关Makefile的问题-什么是"$ +"? & .c文件/依赖项在哪里叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!