【问题标题】:Is it possible to have gnu make check for a files existence?是否可以让 gnu 检查文件是否存在?
【发布时间】:2013-06-18 18:52:09
【问题描述】:

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

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

  1. 构建源
    1. 如果出现任何编译错误,请停止对父目录任何当前子目录的构建。
  2. 链接可执行文件
    1. 如果由于缺少存档文件而导致失败,则在链接阶段显示错误。注意:只有当前子目录级别的构建应该显示错误并退出,但整个过程应该继续并移至下一个子目录。
    2. 如果由于现有存档文件中的未定义符号而导致失败,则在链接阶段显示错误

所以现在我让我的孩子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

【问题讨论】:

    标签: linker makefile gnu


    【解决方案1】:

    你可以使用:

    LIB = $(wildcard idontexist.a)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 2018-07-28
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多