Anyone knows a solution that works something like this:
#include <stdio.h>
#include <gcc.h> /* This .h is what I'm looking for. */
int main (void) {
/* variables declaration (...) */
/* The following line is supposed to be equivalent to:
* $ gcc main.c -o main */
results = gcc_compile_and_link("main.c", "main");
/* Now I want to use the warnings and errors to do something.
* For instance, I'll print them to the console: */
printf("warnings:n");
for (i=0; i<results.warns_len; i++)
printf("%sn", results.warings[i].msg);
printf("errorsn");
for (i=0; i<results.errs_len; i++)
printf("%sn", results.errors[i].msg);
/* free memory and finalize (...) */
return 0;
}
I know I can run the command “gcc main.c -o main” in a fork and parse the output… but I was looking for something more ‘reliable‘ like the example above.
0
GCC was explicitly designed to resist being used as a tools base/library. You need to use Clang for this, or call GCC through commandline.
3
libgccjit
Introduced in GCC 5 and is still experimental as of GCC 6.
Docs: https://gcc.gnu.org/onlinedocs/jit/
Related questions:
- https://stackoverflow.com/questions/8144793/gcc-for-parsing-code
- https://stackoverflow.com/questions/1735360/compiling-program-within-another-program-using-gcc
It’s not possible with gcc but you might find tcc (an embeddable C compiler) good enough for what you have in mind. The distribution comes with a libtcc library that allows to compile, link and run C code “on the fly”.
Note that this only for C, your question is also tagged C++ but I’ve not seen any equivalent of tcc for C++.
1
(I am guessing you are on some POSIX system, like Linux or MacOSX)
You obviously should look into GCCJIT, as mentioned by Ciro Santilli. You’ll then build some AST-like representation of the generated code. Of course you might consider LLVM instead, or even some simpler JIT library like libjit or GNU lightning (but libjit
and lightning
are emitting code quickly, but the emitted code is slow and unoptimized).
However, you might still consider emitting some C code in a temporary file and forking a compilation of it (e.g. as shared library that you would later dynamically load as a plugin using dlopen(3) & dlsym(3)), see here & here for details.
Notice an important fact: generating optimized code takes CPU time (with GCCJIT, or LLVM, or by running gcc -O2
) because it is a difficult task. So the overhead of forking a gcc
process (or using some other compiler, like clang
) is negligible (w.r.t. using some library like GCCJIT or LLVM).
Actually, my experience (in GCC MELT) is that on current desktops and laptops, emitting a few hundred lines of C code and forking a compilation of them is fast enough (one or two tenth of seconds) to be compatible with user interaction. So today, you could consider having a REPL which would do that. See also this related answer.
Look also into Common Lisp and SBCL which is an implementation which compiles into machine code at every REPL interaction.
NB: Seeking partners interested by RefPerSys
I doubt there’s anything better than forking gcc. You might consider clang, which is more designed for this kind of use.
You should obviously look at clang. Compiling code at runtime was a design goal for clang. For example, Apple uses clang to compile OpenGL, OpenCL, Metal, JavaScript.
Gcc on the other hand had the explicit goal of not allowing this (which is why Xcode doesn’t use gcc for many years).