PHP には という便利な関数がありますproc_open
。stdin
実行可能ファイルを呼び出し、そのを開き、パイプとして使用できstdout
ますstderr
。
C++ でこの関数の優れたクロスプラットフォーム バージョンはありますか? グーグルで検索できるのは、Windows 用のこのチュートリアルだけです (ただし、このチュートリアルのコードはハングするだけです)。
あなたはおそらく「どこか」を得ることができます
popen
( http://linux.die.net/man/3/popen )
pstreams ライブラリ(POSIX プロセス制御) - 私はこれを使った経験はありませんが、しっかりしているように見え、Jonathan Wakely によって書かれました。
ブースト プロセス ( http://www.highscore.de/boost/process/、まだブーストされていません)
Poco::Process launch
( http://www.appinf.com/docs/poco/Poco.Process.html#13423 )
static ProcessHandle launch(
const std::string & command,
const Args & args,
Pipe * inPipe,
Pipe * outPipe,
Pipe * errPipe
);
編集:
私が見ることができるように、Boost.Process はもはやアクティブな開発ではなく、例は現在の (1.54) およびそれほど最新ではない (1.4x - ブーストをアップグレードする前に正確なバージョンを書き留めるのを忘れた) バージョンでコンパイルされていません。のブーストなので、推奨事項を撤回する必要があります。
元の投稿
使用できるBoost.Processライブラリがあります。ここで良い例を見つけることができます。また、この章だけでなく、ここからこの章を確認してください。
//
// 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;
}