Compiling C++ code stored in a Ruby string without writing it into a file -
i'm writing ruby code generates string containing c++ program. instance ruby string may contain:
#include <iostream> using namespace std;int main(){cout<<"hello world"<<endl;return 0;}
in order run c++ program stored in ruby string, write string file named c_prog.cpp, use:
%x( g++ c_prog.cpp -o output )
to compile c++ program in file, use:
value = %x( ./output )
and print value.
since c++ program stored in ruby string long (thousands of loc), writing file wastes time. there way can compile program stored in string without writing file? mean like:
%x( g++ 'the ruby string' -o output )
instead of:
%x( g++ c_prog.cpp -o output )
you can pass in single dash file name tell g++
read stdin. so,
%x( echo 'the ruby string' | g++ -o output -x c++ - )
should trick. see this related question.
Comments
Post a Comment