複数のC++をコンパイルしたものをつなげる(例:a.out, b.out, ...)

3パターン書いてみた。

Pythonでつなげる

  • Pythonのsystemからa.outなどを実行して出力をファイルに書き、別の行でb.outを呼ぶ
  • 参考:「グルー言語」

windows batを書く

  • これもシンプル

Unix (Linux)のパイプを使う

  • 想定 a.cppとb.cppがあり、コンパイルしたものをa.out, b.outとする
  • a.outで何か標準出力をする
  • b.outで標準入力を受け取り、標準出力をする
// a.cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    cout << "I am a's output" << endl;
    return 0;
}
// b.cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    getline(cin, s);
    cout << "b got a message " << s << endl;
    return 0;
}
// コンパイル
g++ a.cpp -o a.out
g++ b.cpp -o b.out
// パイプでつなげる(aの出力をbの入力とする)
./a.out | ./b.out

// 出力(ターミナルの表示)はこうなる
b got a message I am a's output
  • 3つ目がテキスト書き出しをスキップできるので速度的には有利かな?