tcc (Tiny C Compiler)
Tiny C Compiler (TCC) is a small, fast C compiler by the creator of QEMU and ffmpeg (Fabrice Bellard). It is ANSI C99 compliant (with some GNU extensions).
It is distributed under the GNU Lesser General Public License (LGPL 2.1).
libtcc
With libtcc, TCC can be used as a backend for dynamic code generation.
#include <libtcc.h> // create & configure tcc compilation context TCCState *tcc_state = tcc_new(); if (!tcc_state) { /* error handling */ } tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY); // add include paths & system libraries if needed //tcc_add_include_path(tcc_state, "/usr/local/include"); //tcc_add_library_path(tcc_state, "/usr/local/lib"); //tcc_add_library(tcc_state, "m"); // link with math library // compile program from a string containing C code #define SOURCE_STR(x) "#include <stdio.h>\n" #x const char *my_program = SOURCE_STR( int add(int a, int b) { printf("Dynamic function called with: %d and %d\n", a, b); return a + b; }); int compiled = tcc_compile_string(tcc_state, my_program); if (compiled == -1) { /* error handling */ } // relocate compiled code (making it executable) if (tcc_relocate(tcc_state) < 0) { /* error handling */ } // get function symbol & call it int (*add_func)(int, int) = (int (*)(int, int)) tcc_get_symbol(tcc_state, "add"); if (!add_func) { /* error handling */ } int result = add_func(5, 7); printf("Result: %d\n", result); tcc_delete(tcc_state); // clean up