Foreach 循环在 Makefile 中不起作用:“系统找不到指定的文件”

发布于 2024-08-16 17:46:52 字数 488 浏览 1 评论 0原文

我有一个包含以下内容的 Makefile:

NUMBERS = 1 2 3 4
lib:
    $(foreach var,$(NUMBERS),./a.out $(var);)

这是我运行的命令(与 Makefile 位于同一目录中)

make -f Makefile

但我收到一条错误消息,指出“系统找不到指定的文件”。

遵循

a.out
1.out
2.out
3.out
4.out

现在错误变为:

./a.out 1; ./a.输出2; ./a.输出3; ./a.输出4; make (e=-1): 错误-1 make: *** [lib] 错误-1

注意:我在 Windows XP 平台上运行

I have a Makefile of the following content:

NUMBERS = 1 2 3 4
lib:
    $(foreach var,$(NUMBERS),./a.out $(var);)

And this is the command that I run ( in the same directory as the Makefile)

make -f Makefile

But I got an error message saying that "The system cannot find the file specified".

Following the suggestion of one of the answers, I created the following file inside the same directory as the Makefile:

a.out
1.out
2.out
3.out
4.out

Now the error becomes:

./a.out 1; ./a.out 2; ./a.out 3;
./a.out 4; make (e=-1): Error -1 make:
*** [lib] Error -1

Note: I am running on Windows XP platform

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

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

发布评论

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

评论(5

§普罗旺斯的薰衣草 2024-08-23 17:46:52

make 的目的是通过运行命令来创建(和更新)依赖于源文件的目标文件。

在这里,问题出在运行的命令上。您正在尝试运行(通过 make)命令 a.out 但它不存在,或者不是可执行命令。尝试将 makefile 中的 a.out 替换为您要运行的实际可执行命令。

The purpose of make is to create (and update) target files that depends on source files by running commands.

Here, the problem is with the command that is run. You are trying to run (through make) the command a.out but it does not exist, or is not an executable command. Try to replace a.out in your makefile by the actual executable command you want to run.

↘紸啶 2024-08-23 17:46:52

在 Windows/DOS 上,使用 && 而不是 ; 将多个命令连接到一行。您必须手动包含最终命令,否则尾随的 && 将引发语法错误。尝试类似的方法:

NUMBERS = 1 2 3 4
lib:
    $(foreach var,$(NUMBERS),.\a.out $(var) && ) echo.

On Windows/DOS, use && instead of ; to join multiple commands on one line. You have to manually include a final command or the trailing && will throw a syntax error. Try something like:

NUMBERS = 1 2 3 4
lib:
    $(foreach var,$(NUMBERS),.\a.out $(var) && ) echo.
看海 2024-08-23 17:46:52

在我看来,错误的出现是因为无法找到文件a.out,而不是因为找不到makefile。

另外,如果 makefile 的名称是“Makefile”,则只需调用“make”就足够了(不使用 -f 选项),因为默认情况下 make 会按名称查找文件:GNUmakefile、makefile 和 Makefile 按此顺序。

It seems to me that the error comes because the file a.out cannot be located and not because the makefile could not be found.

Also if the name of your makefile is "Makefile" just invoking "make" is enough (without using -f option) as make by default would look for a file by names: GNUmakefile, makefile, and Makefile in that order.

假扮的天使 2024-08-23 17:46:52

你到底想做什么?

在我看来,一个简单的脚本比使用 make 更适合。

Just what are you trying to do?

It seems to me that a plain script would be better suited rather than using make.

岁月蹉跎了容颜 2024-08-23 17:46:52

我发现 bta 的答案最有用,但它在 Windows 和 Linux 上都不适用于我,所以我发现一种删除最后的 && 的方法,这样就不需要在两个平台上都工作的无操作命令:

NUMBERS = 1 2 3 4
lib:
    $(filter-out &&EOL, $(foreach var,$(NUMBERS), .\a.out $(var) &&)EOL)

当然要小心数组中与 && 匹配的元素;EOL,但就我而言,这不是问题。

I found the answer by bta most useful, but it didn't work for me on both Windows and Linux, so I found a way to remove the final &&, which avoids the need for a no-op command that works on both platforms:

NUMBERS = 1 2 3 4
lib:
    $(filter-out &&EOL, $(foreach var,$(NUMBERS), .\a.out $(var) &&)EOL)

Of course be careful of elements within your array matching &&EOL, but in my case, this isn't a problem.

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