C ++ proc_open analogue

时间:2013-09-11 19:31:06

标签: php c++ cross-platform popen stdio

在PHP中有一个名为proc_open的便捷功能。它可用于调用可执行文件,将其stdinstdoutstderr作为管道打开。

C ++中是否有这个功能的良好跨平台版本?唯一可转让的东西是Windows的this教程(虽然它的代码只是挂起)。

2 个答案:

答案 0 :(得分:3)

你可能会在某个地方找到

答案 1 :(得分:1)

修改

正如我所看到的,Boost.Process不再处于活动开发状态,并且示例没有使用当前版本(1.54)进行编译而不是当前版本(1.4x - 我忘记在升级之前写下确切的版本提升版本的提升,所以我需要收回我的建议。

原帖

您可以使用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; 
}