[英]C++ proc_open analogue
There's a handy function in PHP called proc_open
. PHP中有一个方便的函数,称为
proc_open
。 It could be used to call an executable, opening its stdin
, stdout
and stderr
as pipes. 它可以用来调用可执行文件,将其
stdin
, stdout
和stderr
作为管道打开。
Is there a good cross-platform version of this function in C++? C ++中是否有此函数的跨平台版本? The only googlable thing is this tutorial for Windows (though code from it just hangs).
唯一可以谷歌浏览的东西是此 Windows教程(尽管其中的代码只是挂起)。
You could probably get 'somewhere' with 您可能会与“某处”
popen
( http://linux.die.net/man/3/popen ) popen
( http://linux.die.net/man/3/popen )
pstreams library (POSIX process control) - I have no prior experience with this, but it looks solid and was written by Jonathan Wakely pstreams库 (POSIX流程控制)-我对此没有任何经验,但是看起来很扎实,由乔纳森·韦克利(Jonathan Wakely)编写
Boost Process ( http://www.highscore.de/boost/process/ , not in boost yet) 增强处理( http://www.highscore.de/boost/process/ ,尚未增强)
Poco::Process launch
( http://www.appinf.com/docs/poco/Poco.Process.html#13423 ) Poco :: Process
launch
( http://www.appinf.com/docs/poco/Poco.Process.html#13423 )
static ProcessHandle launch( const std::string & command, const Args & args, Pipe * inPipe, Pipe * outPipe, Pipe * errPipe );
Edit: 编辑:
As I can see, Boost.Process is no longer in the active development, and the examples are not compiling with the current (1.54) and not so current (1.4x - I forgot to write down the exact version before I upgraded boost) versions of boost, so I need to retract my recommendation. 如我所见,Boost.Process不再处于活动开发中,并且示例未使用当前版本(1.54)进行编译,也不是当前版本(1.4x-在升级Boost之前我记下了确切的版本)版本的提升,所以我需要撤消我的建议。
Original post 原始帖子
There is Boost.Process library which you could use. 有可以使用的Boost.Process库。 You can find good example here .
你可以在这里找到很好的例子。 Also, check this chapter from here , as well as this .
此外,检查这一章从这里 ,还有这个 。
//
// Boost.Process
// ~~~~~~~~~~~~~
//
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
// Copyright (c) 2008 Boris Schaeling
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/process.hpp>
#include <string>
#include <vector>
#include <iostream>
namespace bp = ::boost::process;
bp::child start_child()
{
std::string exec = "bjam";
std::vector<std::string> args;
args.push_back("--version");
bp::context ctx;
ctx.stdout_behavior = bp::capture_stream();
return bp::launch(exec, args, ctx);
}
int main()
{
bp::child c = start_child();
bp::pistream &is = c.get_stdout();
std::string line;
while (std::getline(is, line))
std::cout << line << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.