提升进程流为空
Boost process stream is empty
本文关键字:进程 更新时间:2023-10-16
我刚刚安装了 Boost 库,我正在尝试完成基本教程。
我正在尝试打开一个运行g++ --version
并将输出通过管道传输到std_ std_out
的进程。
代码是从教程中复制的,但进行了以下更改:
- 添加了
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")
相关文章:
- boost::进程间消息队列引发错误
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 是否可以通过C++扩展强制多个python进程共享同一内存
- IPC使用多个管道和分支进程来运行Python程序
- 异常属于C++中的线程还是进程
- WMI检测进程创建事件-c++
- c++多进程编写一个唯一的文件
- 如何在C++中将函数发送到另一个进程
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 终止 QProcess 不会终止子进程
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- Windows 进程间同步类似事件?
- 在挂钩启动新线程时解除挂钩进程
- pclose() 不会给我进程退出代码
- 运行代码时,c++ 会终止进程
- 可以读入进程内存的最大块大小是多少?
- 枚举进程模块在有效句柄上返回无效句柄
- 如何读取特定地址的进程内存?
- 如何从另一个进程解开全局钩子?