【问题标题】:How to fix this simple Makefile error?如何修复这个简单的 Makefile 错误?
【发布时间】:2015-09-04 01:27:36
【问题描述】:

我正在尝试创建一个非常简单的 makefile,它将构建我的 C++ 代码以及创建一个 .txt 文件

我希望将可执行文件和 .txt 文件放在名为“build”的目录中。为此,我需要确保构建目录不存在。我收到语法错误,我似乎无法修复它。这可能是一个非常明显的错误,因为我是创建 makefile 的新手。

谁能帮忙?

错误:

/bin/sh: -c: line 1: syntax error: unexpected end of file make: *** [mlib] Error 2

Makefile:

all:
    if [ ! -d build ]; then
        mkdir build
    fi
    g++ src/*.cpp -o build/mlb
    touch build/mlib_history.txt

【问题讨论】:

  • 这个 makefile 没有利用 任何 make 为你做的事情,它可以作为一个 shell 脚本。
  • 另请参阅.ONESHELL 特殊目标,但正如@EtanReisner 所说,您的配方正在重新发明轮子,让 makefile 已经可以为您处理。

标签: c++ shell makefile


【解决方案1】:

我实际上只是通过将 makefile 更改为:

全部:

if [ ! -d build ]; then mkdir build; fi
#"mkdir build" moved onto the above line
#somehow this fixed it...?
g++ src/*.cpp -o build/mlb
touch build/mlib_history.txt

【讨论】:

  • makefile 配方中的每一行都在其自己的 shell 中运行。要让 shell 命令跨越多行,您需要告诉 make 继续该行(在行尾带有反斜杠)。
  • 似乎更容易做到mkdir -p build
【解决方案2】:

这里有一个稍微好一点的解决方案……

.PHONY: all

BUILD_DIR := build

all:
    [ ! -d $(BUILD_DIR) ] && mkdir $(BUILD_DIR)
    g++ src/*.cpp -o $(BUILD_DIR)/mlb
    touch $(BUILD_DIR)/mlib_history.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多