Yes, it can be done and is actually quite simple. You output the source code to a temporary file ( mkstmp
), you fork
/ exec
the compilation process (which is output to a temporary file), you fork/exec
resulting program, before which you dup2
and pipe
, to connect the input and output. This is basic Unix programming, in C there is nothing complicated.
Generating the code itself can be harder to get, but it depends a lot on the project.
In addition, we have modern tools since two months: I believe that Clang can be something like what you can look at things. If the generation of the code you plan to do is simple (or not simple, but structured), you can also directly output LLVM code. It is not complicated and allows you to generate efficient only compiled time code.
source share