I'm compiling some C++ code at runtime, which I'm then calling in a sort of plugin system, see also my other question. What I do is I create the source code, write it to a file, compile that file and write the output to another file. However, this process feels kinda ugly so I was hoping for some input.
//Open a file
std::ofstream fout("SOURCECODEPATH");
//Write actual function to file
fout << "extern \"C\" void testFunc(float testArray[]) {\n"
" testArray[0] = 1.0;\n"
" testArray[1] = 2.0;\n"
" testArray[2] = 3.0;\n"
"}" << std::endl;
//Compile the file, and write the stdout and stderr to PROCESSOUTPUTPATH using "&>"
system("c++ -shared -fPIC -std=c++14 SOURCECODEPATH -o COMPILEDLIBRARYPATH &> PROCESSOUTPUTPATH");
//Read PROCESSOUTPUTPATH (not implemented)
Currently it's creating 3 files, SOURCECODEPATH, COMPILEDLIBRARYPATH, and PROCESSOUTPUTPATH. However, I would much rather not have the SOURCECODEPATH and PROCESSOUTPUTPATH written to the OS, but rather have them used internally. So pipe (?) the sourcecode to the process and get back the output (preferable split into stderr and stdout). What would be the easiest way to do this?