3

Using TinyCC in my C program lets me use C as a sort of scripting language, reload C files on the fly, and do a lot of fairly neat things... But, one thing is really bothering me. Linking.

I do my normal tcc_new, and tcc_set_output_type with TCC_OUTPUT_MEMORY, but if I don't include a lot of these:

    tcc_add_symbol(tcc_ctx, "printf", &printf);
    tcc_add_symbol(tcc_ctx, "powf", &powf);
    tcc_add_symbol(tcc_ctx, "sinf", &sinf);

everything is very limited.

I want a way to automatically bring in all symbols in the host program. I don't want to have to manually link in every last function in libc, and libm. What mechanisms exist to facilitate auto linking, or adding of symbols. How can I use libm in my code without manually dropping in every last component.

I'm currently using GCC, but on another platform use Visual Studio to compile my program. I could switch entirely to TCC.

1 Answers1

1

TCC comes with a rudimentary runtime library libtcc1. It includes basic functions like those you mention. Therefore, in most cases you can replace all your calls with a single tcc_add_library(tcc_ctx, "libtcc1.a").

libtcc1 is not complete, so you might have to add manually some functions.

mikijov
  • 1,552
  • 24
  • 37
  • libtcc1.a does not contain implementations of `printf()`, `powf()`, `sinf()`, etc. Rather, it is a set of helpers (starting with double underscores) that define functions which do work that the compiler has decided is easier to implement with a function call vs. doing in code generation. See the [files in the lib/ subdirectory](https://repo.or.cz/tinycc.git/tree/HEAD:/lib), helpers like `__va_start`, etc. So if you see linkages of things like printf() working, they are coming from the standard libc, since you're not using -nostdlib. Run `tcc -vv` to see what I'm talking about. – HostileFork says dont trust SE Dec 09 '18 at 19:54