我刚刚安装了Boost库,我正在尝试通过基础教程。
我正在尝试打开一个运行g++ --version
的进程,并将输出传递给std _ std_out
。
从tutorial复制代码并进行以下更改:
cout
语句以跟踪进度bp::find_executable_in_path()
以下是代码:
//
// 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;
using namespace std;
bp::child start_child() {
string exec = bp::find_executable_in_path( "g++" );
cout << "full path is " << exec << endl;
cout << "starting child process" << endl;
vector<std::string> args;
args.push_back( "--version" );
bp::context ctx;
ctx.stdout_behavior = bp::capture_stream();
ctx.stderr_behavior = bp::capture_stream();
return bp::launch( exec, args, ctx );
}
int main() {
bp::child c = start_child();
bp::pistream &is = c.get_stdout();
cout << ( is ? "stream is valid" : "stream is NOT valid" ) << endl;
string line;
cout << "entering read/write loop" << endl;
while( getline( is, line ) ) {
cout << "copying a line" << endl;
cout << line << endl;
}
cout << "exiting read/write loop" << endl;
}
以下是我在命令行上运行g++ --version
时看到的内容:
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这是已编译程序的输出:
full path is /usr/bin/g++
starting child process
stream is valid
entering read/write loop
exiting read/write loop
它永远不会进入读/写循环。流中的数据发生了什么变化?
答案 0 :(得分:1)
您需要输入gcc的完整路径,或使用bp::find_executable_in_path("g++")
[编辑]哎呀。在Windows下:
bp::find_executable_in_path("g++.exe")