是否有可能让gnu检查文件是否存在?

时间:2013-06-18 18:52:10

标签: linker makefile gnu

在链接之前是否可以让make检查文件?

我有一个makefile系统,其顶级Makefile会调用其他子目录并在其中发出make
我的系统的目标是:

  1. 构建源代码
    1. 如果存在任何编译错误,请停止在父目录任何当前子目录上构建。
  2. 链接可执行文件
    1. 如果由于缺少存档文件而出现故障,则在链接阶段显示错误。注意:只有当前子目录级别的构建应该显示错误并退回,但整个过程应该继续并移动到下一个子目录。
    2. 如果由于现有存档文件中未定义的符号而导致失败,则在链接阶段显示错误
  3. 所以现在我让我的孩子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   
    

    childdir/的Makefile是:

    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
    

1 个答案:

答案 0 :(得分:0)

您可以使用:

LIB = $(wildcard idontexist.a)

如果存在,将扩展为文件名,如果不存在,则为空。