#include #include #include #include #include #include typedef struct tcc_state tcc_state; struct tcc_state { TCCState *s; tcc_state *prev; tcc_state *next; }; const char *files[] = { "test.c", "test2.c", NULL }; tcc_state *states = NULL; tcc_state *last_state = NULL; int is_ready = 0; void print_sym(void *ctx, const char *name, const void *val) { printf("symbol: %s, address: %p\n", name, val); is_ready = 1; } tcc_state *make_state(TCCState *s) { tcc_state *new_state = malloc(sizeof(tcc_state)); memset(new_state, 0, sizeof(tcc_state)); (last_state) ? (last_state->next = new_state) : (states = new_state); new_state->s = s; new_state->prev = (last_state) ? last_state : NULL; new_state->next = NULL; last_state = new_state; return new_state; } void free_states(tcc_state *st) { tcc_state *state; TCCState *s; if (st != NULL) { state = st; s = st->s; st = st->next; memset(state, 0, sizeof(tcc_state)); tcc_delete(s); free(state); state = NULL; free_states(st); } } void cleanup() { if (states) { free_states(states); } } int main(int argc, char **argv) { int (*start)(void); tcc_state *st = make_state(tcc_new()); tcc_set_output_type(st->s, TCC_OUTPUT_MEMORY); for (int i = 0; files[i]; i++) { /*printf("files[%i]: %s\n", i, files[i]);*/ if (tcc_add_file(st->s, files[i]) < 0) { printf("oof, press f, failed to compile %s.\n", files[i]); cleanup(); return 1; } } tcc_relocate(st->s, TCC_RELOCATE_AUTO); /*while (!is_ready) { tcc_list_symbols(st->s, NULL, &print_sym); } is_ready = 0;*/ start = tcc_get_symbol(st->s, "start"); /*printf("start: %p\n", start);*/ int ret = start(); cleanup(); return 0; }