Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

3 Answers 3

up vote 7 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
    
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

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

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

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.