2013-04-21 77 views
3

我使用升壓過程並在main tutorials page中使用默認代碼。在C++中使用升壓過程庫輸出

我已經運行這個代碼,它沒有打印任何輸出!

#include <boost/process.hpp> 
#include <string> 
#include <vector> 
#include <iostream> 

namespace bp = ::boost::process; 

int main() 
{ 
    std::string exec = "bjam"; 

    std::vector<std::string> args; 
    args.push_back("--version"); 

    bp::context ctx; 
    ctx.stdout_behavior = bp::capture_stream(); 

    bp::child c = launch(exec, args, ctx); 

    bp::pistream &is = c.get_stdout(); 
    std::string line; 
    while (std::getline(is, line)) 
    std::cout << line << std::endl; 
} 

任何人都可以幫我解決這個問題嗎?

此密碼退出here

謝謝!

回答

1

您可能沒有檢查過程是否成功啓動。我可以簡單地使用/bin/ls具有良好的而不是成功:

// 
// 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 = "/bin/ls"; 

    std::vector<std::string> args; 
    args.push_back("-ltrah"); 

    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; 
} 

注意,使用"ls"失敗 - 沒有錯誤消息。