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 */
24 " return fib(n-1) + fib(n-2);\n"
29 " printf(\"Hello World!\\n\");\n"
30 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
31 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
35 int main(int argc
, char **argv
)
44 fprintf(stderr
, "Could not create tcc state\n");
48 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
49 if (argc
== 2 && !memcmp(argv
[1], "lib_path=",9))
50 tcc_set_lib_path(s
, argv
[1]+9);
52 /* MUST BE CALLED before any compilation */
53 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
55 if (tcc_compile_string(s
, my_program
) == -1)
58 /* as a test, we add a symbol that the compiled program can use.
59 You may also open a dll with tcc_add_dll() and use symbols from that */
60 tcc_add_symbol(s
, "add", add
);
62 /* get needed size of the code */
63 size
= tcc_relocate(s
, NULL
);
67 /* allocate memory and copy the code into it */
71 /* get entry symbol */
72 func
= tcc_get_symbol(s
, "foo");
76 /* delete the state */