2 * Simple Test program for libtcc
4 * libtcc can be useful to use tcc as a "backend" for a code generator.
11 void handle_error(void *opaque
, const char *msg
)
13 fprintf(opaque
, "%s\n", msg
);
16 /* this function is called by the generated code */
22 /* this strinc is referenced by the generated code */
23 const char hello
[] = "Hello World!";
26 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
27 "extern int add(int a, int b);\n"
28 "#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */
29 " __attribute__((dllimport))\n"
31 "extern const char hello[];\n"
37 " return fib(n-1) + fib(n-2);\n"
42 " printf(\"%s\\n\", hello);\n"
43 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
44 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
48 int main(int argc
, char **argv
)
56 fprintf(stderr
, "Could not create tcc state\n");
60 /* set custom error/warning printer */
61 tcc_set_error_func(s
, stderr
, handle_error
);
63 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
64 for (i
= 1; i
< argc
; ++i
) {
68 tcc_set_lib_path(s
, a
+2);
70 tcc_add_include_path(s
, a
+2);
72 tcc_add_library_path(s
, a
+2);
76 /* MUST BE CALLED before any compilation */
77 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
79 if (tcc_compile_string(s
, my_program
) == -1)
82 /* as a test, we add symbols that the compiled program can use.
83 You may also open a dll with tcc_add_dll() and use symbols from that */
84 tcc_add_symbol(s
, "add", add
);
85 tcc_add_symbol(s
, "hello", hello
);
87 /* relocate the code */
88 if (tcc_relocate(s
) < 0)
91 /* get entry symbol */
92 func
= tcc_get_symbol(s
, "foo");
99 /* delete the state */