【发布时间】:2013-09-15 23:29:56
【问题描述】:
PHP 中有一个方便的函数,称为proc_open。它可用于调用可执行文件,将其stdin、stdout 和stderr 作为管道打开。
这个函数在 C++ 中有没有好的跨平台版本?唯一可以谷歌搜索的是this Windows 教程(尽管其中的代码只是挂起)。
【问题讨论】:
标签: php c++ cross-platform popen stdio
PHP 中有一个方便的函数,称为proc_open。它可用于调用可执行文件,将其stdin、stdout 和stderr 作为管道打开。
这个函数在 C++ 中有没有好的跨平台版本?唯一可以谷歌搜索的是this Windows 教程(尽管其中的代码只是挂起)。
【问题讨论】:
标签: php c++ cross-platform popen stdio
你可能会得到“某处”
popen (http://linux.die.net/man/3/popen)
pstreams library(POSIX 流程控制) - 我之前没有这方面的经验,但它看起来很可靠,由 Jonathan Wakely 编写
升压过程(http://www.highscore.de/boost/process/,尚未升压)
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
);
【讨论】:
编辑:
正如我所看到的,Boost.Process 不再处于积极开发中,并且示例没有使用当前 (1.54) 进行编译,也不是最新 (1.4x - 我在升级之前忘记写下确切的版本) boost) 版本的 boost,所以我需要撤回我的建议。
原帖
您可以使用Boost.Process 库。你可以找到很好的例子here。另外,检查this chapter 和here,以及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;
}
【讨论】: