gpt4 book ai didi

c++ - Boost 进程流为空

转载 作者:行者123 更新时间:2023-11-28 05:05:46 27 4
gpt4 key购买 nike

我刚刚安装了 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

它永远不会进入读/写循环。流中的数据发生了什么?

最佳答案

您需要输入 gcc 的完整路径,或使用 bp::find_executable_in_path("g++")

[编辑] 糟糕。在窗口下:

bp::find_executable_in_path("g++.exe")

关于c++ - Boost 进程流为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com