【问题标题】:Makefile crashes on target cleanMakefile 在目标清理时崩溃
【发布时间】:2015-08-21 14:09:06
【问题描述】:

我正在尝试用 make 编译一个 c++ 项目,我不是 Makefile 专家,但看起来没问题,这个 Makefile 有什么问题:

.PHONY: all test log debug clean cleanall

####### BUILD RULES #####################################################
all:        cleanall
        make debug
        clean
        make test 
        clean
        make log

log: CFLAGS := $(CFLAGS_LOG)

clean:
    rm -f $(OBJS) 

cleanall: clean
    rm -f   $(OUTPATH)$(APPNAME) \
        $(OUTPATH_DBG)$(APPNAME_DBG) \
        $(OUTPATH_DBG)$(APPNAME_LOG)

test: $(OBJS)
    $(CXX) -static $(CFLAGS) -o $(OUTPATH)$(APPNAME) $(OBJS) $(LIB)
    strip $(OUTPATH)$(APPNAME)

log: $(OBJS)
    $(CXX) -static $(CFLAGS_LOG) -o $(OUTPATH)$(APPNAME_LOG) $(OBJS) $(LIB)
    strip $(OUTPATH)$(APPNAME_LOG)

debug: $(OBJS)
    $(CXX) $(CFLAGS_DBG) -o $(OUTPATH_DBG)$(APPNAME_DBG) $(OBJS) $(LIB)

不断出现崩溃错误:

make[1]: Leaving directory '/blablabla/dcp_edi/test'
clean
make: clean: command not found
Makefile:66: recipe for target 'all' failed

抱歉,您似乎不能发布包含太多代码的问题,而且我不知道该写什么才能使这篇文章更长。

【问题讨论】:

  • clean 不是 shell 命令。它是你的makefile中的目标。 all: make clean,基本上
  • 如果我添加 all: cleanall make debug make clean make test make clean make log 它会为目标测试产生同样的错误。

标签: c++ makefile


【解决方案1】:

您的问题出现在这里:

####### BUILD RULES #####################################################
all:        cleanall
        make debug
        clean <<<<<<<<<<<
        make test 
        clean <<<<<<<<<<<
        make log

规则部分之后的所有内容都被 make 解释为要执行的 shell 命令。正如评论中提到的clean 不是shell 命令,而是makefile 中定义的另一个规则。

你可以通过另一个递归调用来解决这个问题:

####### BUILD RULES #####################################################
all:        cleanall
        make debug
        make clean
        make test 
        make clean
        make log

【讨论】:

  • 如果我添加 all: cleanall make debug make clean make test make clean make log 它会为目标测试产生同样的错误。
  • Makefile:93: recipe for target 'test' failed make[1]: *** [test] Error 1 make[1]: Leaving directory 'blablabla/dcp_edi/test' Makefile:66: recipe for target 'all' failed make: *** [all] Error 2
  • @mattobob 您在这些行之前看到的错误?要么是链接器因错误而退出,要么是 strip 无法从 PATH 变量中解析。无论如何,这不是makefile的问题。
  • 哦,是的,你的权利它没有在机器上找到输出文件夹,但它应该自己创建它:/usr/bin/ld: cannot open output file Release/tmux_mod_edi: file not found
  • @mattobob "但它应该是自己创建的" ld不会创建任何目录,你可以在之前引入另一个规则或操作来创建目录调用链接器。
【解决方案2】:

你应该写:

all: cleanall debug clean test clean log

(没有任何其他行)。问题是,clean 不会在这里执行两次。如果这不是真的需要,只需删除第一个clean。否则,您可能会将clean 目标拆分为clean-objsclean-test。如果你不能拆分它,你就无法绕过 submake 调用(正如已经建议的那样)。

我只是想强调解决这个问题的干净和标准的方法。

【讨论】:

    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多