Created
March 9, 2020 20:38
-
-
Save llandsmeer/3489603e5070ee8dbe75cdf1865a5ca9 to your computer and use it in GitHub Desktop.
LibTCC demo - usage example for live c code compilation & execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <libtcc.h> | |
void callme(int x) { | |
printf("hello, %d\n", x); | |
} | |
void error_func(void * user, const char * msg) { | |
printf("TCC Error: %s\n", msg); | |
exit(1); | |
} | |
int main(int argc, char ** argv) { | |
int err; | |
TCCState * s = tcc_new(); | |
tcc_set_output_type(s, TCC_OUTPUT_MEMORY); | |
tcc_compile_string(s, "void callme(int); int main() { callme(42); }"); | |
tcc_set_error_func(s, 0, error_func); | |
tcc_add_symbol(s, "callme", callme); | |
tcc_run(s, argc, argv); | |
tcc_delete(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks llandsmeer for the example! It shows that it's possible to compile a code in run time and run it. Kind of being equivalent to the "eval" in some scripting languages.
Here is what I did to get it compiled and run (YMMV).
Checked out the source code of tcc (link here),
untar and build it with
./configure; make
cd tcc-0.9.27
sudo mkdir /usr/local/lib/tcc
sudo cp libtcc*.c /usr/local/lib/tcc
Compile the above example code (put in a file called testme.c)
gcc -I . testme.c -L . -ltcc -ldl
./a.out