makefile -/bin/sh : 1: Syntax error: word unexpected (expecting ")")

标签 makefile gnu-make

我有这个 Makefile:

TMP_DIR := tmp
RUN_OR_NOT := $(shell date '+%y%m%d%H%M')

all: version

version:
    ifeq ($(shell test -d ${TMP_DIR} && echo -n "yes";),yes)
        $(shell echo ${TMP_DIR} already exists ...)
    else
        $(shell mkdir -p ${TMP_DIR})
    endif

我想先检查目录 tmp 是否存在,如果不存在则创建它。这有效,但有一个奇怪的错误:

 ifeq (yes,yes)
 /bin/sh: 1: Syntax error: word unexpected (expecting ")")
 Makefile:7: die Regel für Ziel „version“ scheiterte
 make: *** [version] Fehler 2

为什么会出现这个奇怪的/bin/sh: 1: Syntax error: word unexpected (expecting ")")错误?以及如何解决这个问题?

最佳答案

在 makefile 中,配方是一个 shell 脚本。您正在尝试将诸如 ifeq 之类的 make 结构放入您的配方中。 Make 会将这些传递给 shell,而 shell 会抛出此错误,因为它不理解 makefile 语法。

您应该使用 shell 脚本而不是 makefile 语法来编写您的食谱:

version:
        if test -d ${TMP_DIR}; then \
            echo ${TMP_DIR} already exists ...; \
        else \
            mkdir -p ${TMP_DIR}; \
        fi

虽然我不知道你为什么关心目录是否已经存在;我个人只会使用:

version:
        mkdir -p ${TMP_DIR}

关于makefile -/bin/sh : 1: Syntax error: word unexpected (expecting ")"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44373872/

相关文章:

docker - 无论--jobs是什么,GNU Make仅使用4个线程

java - 默认: classes in this makefile?是什么意思

windows - 将 GNUMake 与包含空格的文件名一起使用的最可靠方法是什么?

makefile - 在单独的目录中使用相同的 make 文件

visual-c++ - MSVC - 通过 Makefile 创建静态库

c - "Source file is more recent than executable"除了它不是

makefile - $ +在makefile中

c - 使用 makefile 将目标文件放入新目录时遇到问题

makefile - GNU 生成文件 : multiple outputs from single rule + preventing intermediate files from being deleted

Makefile 匹配命令中的模式