【问题标题】:how to exit and log the data in file at same time in makefile when error occurs发生错误时如何在makefile中同时退出并记录文件中的数据
【发布时间】:2016-12-11 12:15:03
【问题描述】:

制作文件

RUN_EXE = xyz.exe
SOME_DIR_PATH = ../folder1/

ifdef $(MAKECMDGOALS)
   ifeq ($(MAKECMDGOALS), "target_1")
      ABC = status.log
      ARGS_TO_EXE = "argc1"
   endif
   ifeq ($(MAKECMDGOALS), "target_1")
      ABC = file2.txt
      ARGS_TO_EXE = "argc2"
   endif
else
   ABC = status.log file2.txt
   ARGS_TO_EXE = ""
endif

 # phony define
.PHONY = default target_1 target_2 

# if target not mention run default
default: $(ABC)


target_1 target_2:$(ABC)

$(ABC):
      $(CD) $(SOME_DIR_PATH) && ( $(RUN_EXE) $(ARGS_TO_EXE) || (exit 1;)) 2>&1 | tee -a ../status.log; \

我有两个使用第一个makefile的makefile,我正在调用这个makefile。 如果出现一些错误,它应该成功返回到第一个 makefile。

出于测试目的,我在可执行文件中创建了一个错误,我试图以不存在的读取模式打开一个文件。所以我的可执行文件将通过错误,它会出来

当我给予时

make target_1

错误:打开文件时出错(使用 c printf 函数,文件中也一样)

make target_2 

错误:打开文件时出错(使用 c printf 函数,文件中也一样)

这里出现问题

make

错误:打开文件时出错(使用 c printf 函数,同样会在文件中)。 错误:打开文件时出错(使用 c printf 函数,同样会在文件中)。

它应该在第一次打印并成功登录 status.log 文件后退出,但它没有发生。 AS $(ABC) 包含两个目标 status.log 和 file2.txt,所以在第一次运行时它会尝试构建 status.log,然后它会尝试构建 file2.txt。

但我希望如果第一次运行时出现错误,它应该返回到第一个 makefile 并将错误也记录在 status.log 中,并且不应该为 file2.txt 运行并使用退出 1 出来

【问题讨论】:

    标签: c bash unix makefile


    【解决方案1】:

    如果出现错误,它应该成功返回到第一个 makefile。

    您的第一个 makefile 可以使用 - 选项调用此 makefile,例如

    run_submake:
        -$(MAKE) <arguments>
    

    这样子生成的错误就被忽略了。


    您的规则似乎不正确。配方必须生成一个文件(或多个文件)或者是虚假目标。例如,应该有生成status.logfile2.txt 的规则。您的 makefile 应如下所示:

    RUN_EXE := xyz.exe
    SOME_DIR_PATH := ../folder1/
    ARGS_TO_EXE := "argc1"
    
    all : status.log file2.txt # The default target.
    target_1: status.log
    target_2: file2.txt
    
    .PHONY: all target_1 target_2
    
    status.log:
        ${CD} ${SOME_DIR_PATH} && ( $(RUN_EXE) $(ARGS_TO_EXE) | tee -a $@; exit $${PIPESTATUS[0]} )
    
    file2.txt:
        # Add a recipe to produce file2.txt
    

    【讨论】:

    • 配方对我来说没有改变,只有可执行文件的参数在改变。这就是为什么我想坚持单一的食谱。我试过你的食谱,但它不会将生成的错误重定向到文件。
    • @RohanG 使用|&amp; 重定向到tee 而不是| 将stdout 和stderr 都重定向到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多