Unix 带有IF语句的Makefile
我有一个Unix 带有IF语句的Makefile,unix,if-statement,makefile,Unix,If Statement,Makefile,我有一个Makefile,它创建了一个名为monitor的程序: fo/monitor: fo/monitor.c fo/inotify.c (cd fo ; $(MAKE) monitor) 我有两种类型的系统可以运行Make,并且只希望有一个安装程序 因此,我想在此添加一个IF语句来检查文件,如果它存在,则构建监视器 fo/monitor: if [ -f path/to/file/exists ]; \ then \ fo/monitor.c fo
Makefile
,它创建了一个名为monitor的程序:
fo/monitor: fo/monitor.c fo/inotify.c
(cd fo ; $(MAKE) monitor)
我有两种类型的系统可以运行Make,并且只希望有一个安装程序
因此,我想在此添加一个IF
语句来检查文件,如果它存在,则构建监视器
fo/monitor:
if [ -f path/to/file/exists ]; \
then \
fo/monitor.c fo/inotify.c \
(cd fo ; $(MAKE) monitor) \
else \
echo "" >/dev/null \
fi \
问题是,当我尝试运行Makefile时,它会崩溃,因为它不喜欢此代码。有人能告诉我正确的方向吗?必须将fo/monitor.c
和fo/inotify.c
添加到目标依赖项中,而不是在if
语句中。您还可以使用make
的-C
选项,而不是使用子shell。你必须在“无”中“无”回应“无”
这应该是好的:
fo/monitor: fo/monitor.c fo/inotify.c
if [ -f path/to/file/exists ]; then \
$(MAKE) -C fo monitor; \
fi
另一种方法是仅当path/to/file/exists
存在时才依赖于该目标:
# add fo/monitor dependency only if path/to/file/exists exists
all : $(shell test -e path/to/file/exists && echo "fo/monitor")
fo/monitor: fo/monitor.c fo/inotify.c
${MAKE} -C ${@D}
好的,它不再抱怨IF
,现在它说***缺少分隔符。停止。
。有什么建议吗?@DustinCook请确保已将4个空格替换为选项卡!是的,我刚刚意识到了这一点!!呜呜!谢谢你的帮助!此配方承诺生成fo/monitor
,但可能不会,因此每次运行make时,它都会继续尝试生成fo/monitor
,因为它不是虚假目标。@MaximYegorushkin您完全是赖特。这就是为什么我建议使用您的回复。这比if语句更简洁+1@jml我必须承认,我对食谱中的if语句有点反感,它们伤害了我的风格感。