/* * Compile source code and call a callback with a pointer to the symbol "f". */ static int run_callback(const char *src, callback_type callback) { TCCState *s; int result; void *ptr; s = tcc_new(); if (!s) return -1; if (tccdir) tcc_set_lib_path(s, tccdir); if (include_dir) { if (tcc_add_include_path(s, include_dir) == -1) return -1; } if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1) return -1; if (tcc_compile_string(s, src) == -1) return -1; if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1) return -1; ptr = tcc_get_symbol(s, "f"); if (!ptr) return -1; result = callback(ptr); tcc_delete(s); return result; }
int main(int argc, char **argv) { TCCState *s; int i; int (*func)(int); s = tcc_new(); if (!s) { fprintf(stderr, "Could not create tcc state\n"); exit(1); } /* if tcclib.h and libtcc1.a are not installed, where can we find them */ for (i = 1; i < argc; ++i) { char *a = argv[i]; if (a[0] == '-') { if (a[1] == 'B') tcc_set_lib_path(s, a+2); else if (a[1] == 'I') tcc_add_include_path(s, a+2); else if (a[1] == 'L') tcc_add_library_path(s, a+2); } } /* MUST BE CALLED before any compilation */ tcc_set_output_type(s, TCC_OUTPUT_MEMORY); if (tcc_compile_string(s, my_program) == -1) return 1; /* as a test, we add a symbol that the compiled program can use. You may also open a dll with tcc_add_dll() and use symbols from that */ tcc_add_symbol(s, "add", add); /* relocate the code */ if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) return 1; /* get entry symbol */ func = tcc_get_symbol(s, "foo"); if (!func) return 1; /* run the code */ func(32); /* delete the state */ tcc_delete(s); return 0; }
TCCState * tcc_init_state(void) { int i; TCCState *tcc_state = NULL; tcc_state = tcc_new(); if (!tcc_state) { fprintf(stderr, "Impossible de creer un contexte TCC\n"); exit(1); } tcc_set_output_type(tcc_state, TCC_OUTPUT_MEMORY); //tcc_add_file(tcc_state, libcodenat_path); for (i=0;i<lib_array_count; i++){ tcc_add_file(tcc_state, lib_array[i]); } for (i=0;i<include_array_count; i++){ tcc_add_include_path(tcc_state, include_array[i]); } return tcc_state; }