Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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("%s\n", results.warings[i].msg);
    printf("errors\n");
    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.

share|improve this question
up vote 8 down vote accepted

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.

share|improve this answer
1  
Do you know the reason for that design decision? – Max Mar 10 '13 at 13:11
    
@Max: Because it would permit those horrific proprietary code people to do the same. – DeadMG Mar 10 '13 at 16:29
    
libgccjit is working in that direction, although it will be an uphill battle: programmers.stackexchange.com/a/323821/124651 – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Jul 2 at 9:45

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

share|improve this answer
    
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. – Basile Starynkevitch Jul 2 at 10:45

I doubt there's anything better than forking gcc. You might consider clang, which is more designed for this kind of use.

share|improve this answer

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

Look also into Common Lisp and SBCL which is an implementation which compiles into machine code at every REPL interaction.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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