Makefile 在后台运行进程

发布于 2024-12-08 12:45:30 字数 386 浏览 0 评论 0原文

我的 Makefile 中有这个:

run:
     for x in *.bin ; do ./$$x ; done

这样它就可以一一启动所有可执行文件。我想这样做:

run:
     for x in *.bin ; do ./$$x &; done

以便它启动每个可执行文件并将其放在后台。当我输入 & 符号时,上述语句出现语法错误。

我不想将 make 调用为 make & 因为这将在后台运行进程,但仍然一个接一个地运行,而我希望单个可执行文件在后台运行,以便在任何情况下即时我有多个可执行文件正在运行。

先感谢您。

I have this in my Makefile:

run:
     for x in *.bin ; do ./$x ; done

such that it launches all executables one by one. I want to do this:

run:
     for x in *.bin ; do ./$x &; done

so that it starts each executable and puts it in the background. I get a syntax error for the above statement when I put the ampersand.

I dont want to invoke the make as make & since this will run processes in the background but still one by one, whereas I want individual executables to run in the background, so that at any instant I have more than one executable running.

Thank you in advance.

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

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

发布评论

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

评论(3

怀念你的温柔 2024-12-15 12:45:30

尝试通过子 shell 执行:

run:
     for x in *.bin ; do (./$x &); done

也许 make -j 是更好的选择。尝试使用如下所示的 Makefile

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    ./$@

然后使用 make -j 执行,其中 是同时执行的数量要运行的作业。

Try to execute via a subshell:

run:
     for x in *.bin ; do (./$x &); done

Maybe make -j is a better option. Try a Makefile that looks something like this:

BINS = $(shell echo *.bin)

.PHONY: $(BINS)
run: $(BINS)

*.bin:
    ./$@

And then execute with make -j <jobs> where <jobs> is number of simultaneous jobs to run.

悲凉≈ 2024-12-15 12:45:30

您收到的语法错误是 shell 语法错误,而不是 make 语法问题。 & 符号实际上是一个 命令终止符/分隔符,就像分号是;因此,表达您想要的 for 循环的方法是:

run:
        for x in *.bin ; do ./$x & done

但是,正如其他人所指出的,在实用的情况下,直接将事物表达为 make 依赖项通常比复杂的 shell 片段和 shell 循环更灵活。

The syntax error you're getting is a shell syntax error, rather than a problem with make syntax. The ampersand is in fact a command terminator/separator, just as semicolon is; so the way to express the for loop you want is:

run:
        for x in *.bin ; do ./$x & done

However, as others have noted, where it's practical it's usually more flexible to express things directly as make dependencies rather than complicated shell snippets and shell loops.

再浓的妆也掩不了殇 2024-12-15 12:45:30

尝试:

run:
    for x in *.bin ; do (./$x &) ; done

() 在子 shell 中运行命令。

Try:

run:
    for x in *.bin ; do (./$x &) ; done

The ()'s run the command in a subshell.

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