是否可以make
在链接之前检查文件?
我有makefile
一个顶层系统,Makefile
可以调用其他子目录和make
其中的问题。
我的系统的目标是:
- 构建源
- 如果有任何编译错误,请停止对父目录和任何当前子目录的构建。
- 链接可执行文件
- 如果由于缺少存档文件而导致失败,则在链接阶段显示错误。注意:只有当前子目录级别的构建应该显示错误并退出,但整个过程应该继续并移动到下一个子目录。
- 如果由于现有存档文件中的未定义符号而导致失败,则在链接阶段显示错误
所以现在我让我的孩子Makefile
做一个“如果构建失败,那么父母失败,如果链接失败,那么父母继续”之类的事情:
#build the source code
$(CC) -o $@ -c $<
#link the executable
-$(CC) $^ -o $@ $(LIB) #the - allows the parent to continue even if this fails
这可行,但是这将允许任何$(LIB)
链接错误通过,如果存档不存在,我只想允许父级继续。
澄清:给定以下目录结构:
C
├── Makefile
└── childdir
├── a.c // This source file uses functions from idontexist.a
└── Makefile
顶层 Makefile 是:
.PHONEY: all
all:
@echo "Start the build!" # I want to see this always
$(MAKE) --directory=childdir # then if this works, or fails because the
# .a is missing
@echo "Do more stuff!" # then I want to see this
Makefile位于childdir/
:
LIB=idontexist.a #This doesn't exist and that's fine
EXE=target
SRC=a.c
OBJS=$(patsubst %.c,%.o,$(SRC))
%.o : %.c
$(CC) -o $@ -c $< #If this fails, I want the ENTIRE build to fail, that's
# good, I want that.
.PHONEY: all
all: $(EXE)
$(EXE):$(OBJS)
-$(CC) $^ -o $@ $(LIB) #If this fails, because $(LIB) is missing
# I don't really care, if it's because we can't find
# some symbol AND the file DOES exist, that's a problem