Makefile 在后台运行进程
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试通过子 shell 执行:
也许
make -j
是更好的选择。尝试使用如下所示的Makefile
:然后使用
make -j
执行,其中
是同时执行的数量要运行的作业。Try to execute via a subshell:
Maybe
make -j
is a better option. Try aMakefile
that looks something like this:And then execute with
make -j <jobs>
where<jobs>
is number of simultaneous jobs to run.您收到的语法错误是 shell 语法错误,而不是 make 语法问题。 & 符号实际上是一个 命令终止符/分隔符,就像分号是;因此,表达您想要的 for 循环的方法是:
但是,正如其他人所指出的,在实用的情况下,直接将事物表达为 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:
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.
尝试:
() 在子 shell 中运行命令。
Try:
The ()'s run the command in a subshell.