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: */
    for (i=0; i<results.warns_len; i++)
        printf("%s\n", results.warings[i].msg);
    for (i=0; i<results.errs_len; i++)
        printf("%s\n", 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.


6 Answers 6


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.



Introduced in GCC 5 and is still experimental as of GCC 6.

Docs: https://gcc.gnu.org/onlinedocs/jit/

Related questions:


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++.

  • Notice that tcc compiles quickly, but is not optimizing at all. The generated code is often 3 to 10 times slower than what gcc -O2 would produce. Jul 2, 2016 at 10:45

(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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.