2 * Simple Test program for libtcc
4 * libtcc can be useful to use tcc as a "backend" for a code generator.
12 /* this function is called by the generated code */
19 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
20 "extern int add(int a, int b);\n"
26 " return fib(n-1) + fib(n-2);\n"
31 " printf(\"Hello World!\\n\");\n"
32 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
33 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
37 int main(int argc
, char **argv
)
45 fprintf(stderr
, "Could not create tcc state\n");
49 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
50 for (i
= 1; i
< argc
; ++i
) {
54 tcc_set_lib_path(s
, a
+2);
56 tcc_add_include_path(s
, a
+2);
58 tcc_add_library_path(s
, a
+2);
62 /* MUST BE CALLED before any compilation */
63 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
65 if (tcc_compile_string(s
, my_program
) == -1)
68 /* as a test, we add a symbol that the compiled program can use.
69 You may also open a dll with tcc_add_dll() and use symbols from that */
70 tcc_add_symbol(s
, "add", add
);
72 /* relocate the code */
73 if (tcc_relocate(s
, TCC_RELOCATE_AUTO
) < 0)
76 /* get entry symbol */
77 func
= tcc_get_symbol(s
, "foo");
84 /* delete the state */