GNU make 产生“命令在第一个目标之前开始”错误

发布于 2024-10-12 03:09:08 字数 601 浏览 0 评论 0原文

在我的 makefile 中,我想检查库是否存在并给出信息丰富的错误消息。我创建了一个条件,当找不到文件时应该退出 make 进程:

 9: ifeq ($(${JSONLIBPATH}),)
10:    JSONLIBPATH = ${ALTJSONLIBDIR}/${LIBJSON}
11: endif
12: ifeq ($(${JSONLIBPATH}),)
13:    $(error JSON library is not found. Please install libjson before building)
14: endif 

My makefile 卡在第 13 行:

Makefile:13: *** commands commence before first target.  Stop.

在第 13 行之后,我的 makefile 有了它的目标。

我尝试将此条件块放入目标中(例如,名为 isJSONLibraryInstalled 的目标),但这无法正确执行。

在处理目标之前,如何检查文件是否存在并处理错误情况?如果这是一个愚蠢的问题,我深表歉意。

In my makefile, I would like to check for the existence of a library and give an informative error message. I created a conditional that should exit the make process when the file is not found:

 9: ifeq ($(${JSONLIBPATH}),)
10:    JSONLIBPATH = ${ALTJSONLIBDIR}/${LIBJSON}
11: endif
12: ifeq ($(${JSONLIBPATH}),)
13:    $(error JSON library is not found. Please install libjson before building)
14: endif 

My makefile gets stuck on line 13:

Makefile:13: *** commands commence before first target.  Stop.

After line 13, my makefile has its targets.

I tried putting this conditional block into a target (e.g. a target called isJSONLibraryInstalled) but this does not execute correctly.

How would I check for a file's existence and handle the error case, before processing targets? Apologies if this is a dumb question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

躲猫猫 2024-10-19 03:09:08

首先,您正在查看以当前路径命名的变量的内容,这可能不是您想要的。简单的环境变量引用是 $(name)${name},而不是 $(${name})。因此,始终会评估第 13 行。

其次,我认为它因 $(error ...) 表达式的缩进而令人窒息。虽然表达式解析为空字符串,但行的开头仍然有一个制表符,它表示命令,而命令又不能存在于规则之外。

我认为使用空格而不是制表符来缩进会有效。

First of all, you are looking at the contents of a variable that is named after the current path, which is probably not what you want. A simple environment variable reference is $(name) or ${name}, not $(${name}). Due to this, line 13 is always evaluated.

Second, I think it is choking on the indentation of the $(error ...) expression. While the expression resolves to an empty string, there is still a tab character at the start of the line, which indicates a command, which in turn cannot exist outside a rule.

I think using spaces rather than tabs to indent would work.

若相惜即相离 2024-10-19 03:09:08

当您收到 Make 错误消息时,请务必检查错误消息文档

在 GNU Make 3.81 上(error 似乎已从较新版本中删除),它显示:

这意味着 makefile 中的第一件事似乎是命令脚本的一部分:它以 TAB 字符开头,并且似乎不是合法的 make 命令(例如变量赋值)。命令脚本必须始终与目标关联。

使事情更加混乱的是“似乎不是合法的 make 命令”部分。这解释了为什么 in:

    a := b
    $(error a)

错误发生在第 2 行而不是第 1 行: make 只是接受它可以解析的语句,例如赋值,因此以下内容有效:

    a := b
a:
    echo $a

注意:SO 目前在代码中将制表符转换为空格,因此您不能仅将上述代码复制到编辑器中。

When you get Make error messages, always check the Error message documentation

On GNU Make 3.81 (error appears to have been removed from newer versions), it says:

This means the first thing in the makefile seems to be part of a command script: it begins with a TAB character and doesn't appear to be a legal make command (such as a variable assignment). Command scripts must always be associated with a target.

What makes matters more confusing is that "doesn't appear to be a legal make command" part. That explains why in:

    a := b
    $(error a)

the error happens at line 2 and not 1: make simply accepts statements that it can parse, like the assignment, so the following works:

    a := b
a:
    echo $a

Note: SO currently converts tabs to spaces in code, so you can't just copy the above code into your editor.

紫轩蝶泪 2024-10-19 03:09:08

对我来说,造成这种情况的连接器之前有一个不必要的空白。
在 slickEdit 上,我选择了查看所有特殊字符的选项,并注意到了害群之马。

For me it was an unnecessary white space before the connector that was causing this.
On slickEdit I selected the option to view all special character and noticed the black sheep.

寻找一个思念的角度 2024-10-19 03:09:08

您可以使用VSCode检查空白空格制表符 [查看>渲染空白]

如您所见;

  • command-1 有一个制表符 (->) 且末尾有 空格
  • command-2>space 首先
  • command-3/4tab+空格

在此处输入图像描述

所以,您应该删除末尾的空格,并使用相同的操作应用相同的空格。例如:选项卡如下所示;

输入图片此处描述

You can check whitespaces, spaces and tabs by using VSCode [View > Render Whitespace]

As you can see;

  • command-1 has a tab (->) and whitespace at the end
  • command-2 has space at first
  • command-3/4 has tab+spaces

enter image description here

So, you should remove the whitespaces at the end and apply the same spaces with the same action. e.g: tab like the following;

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文