#include #include #include #include "libtcc.h" char* my_program[] = { "#include \n" /* include the "Simple libc header for TCC" */ "int sub(int a, int b){return a-b;}\n" "int foo(int n)\n" "{\n" " printf(\"Hello World!\\n\");\n" " printf(\"sub(%d, %d) = %d\\n\", 2*n, n, sub(2*n, n));\n" " return 0;\n" "}\n", "#include \n" /* include the "Simple libc header for TCC" */ "int sub(int a, int b){return a-b;}\n" "int foo(int n){\n" " printf(\"Hello world!\n\");\n" " printf(\"%d\n\", sub(15,10));\n" "}\n" }; int main(int argc, char **argv) { TCCState *s; int (*func)(int); int i; for (i =0; i < sizeof(my_program)/sizeof(my_program[0]); i++) { 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 */ if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) tcc_set_lib_path(s, argv[1]+9); /* MUST BE CALLED before any compilation */ tcc_set_output_type(s, TCC_OUTPUT_MEMORY); if (tcc_compile_string(s, my_program[i]) == -1) return 1; /* relocate the code */ if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) return 1; /* get entry symbol */ func = tcc_get_symbol(s, "foo"); /* run the code */ func(32); /* delete the state */ tcc_delete(s); } return 0; }