C++ GCC Library – How to Use GCC as a Library

cgcc

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.

Best Answer

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.

Related Topic