Skip to content

Instantly share code, notes, and snippets.

@b4284
Created December 5, 2015 19:14
Show Gist options
  • Select an option

  • Save b4284/cbdf993bf95c7f93a0ef to your computer and use it in GitHub Desktop.

Select an option

Save b4284/cbdf993bf95c7f93a0ef to your computer and use it in GitHub Desktop.
#include <libtcc.h>
#include <stdio.h>
#include <stdlib.h>
void gen_addx(char *s, int x) {
sprintf(s, "\
int addx(int y) { \
return (%d + y); \
}", x);
}
int main() {
char prog_str[100];
gen_addx(prog_str, 5);
TCCState *state = tcc_new();
tcc_compile_string(state, prog_str);
int func_size = tcc_relocate(state, NULL);
int (*addx)(int) = malloc(func_size);
int q = tcc_relocate(state, addx);
printf("%d\n", addx(7));
free(addx);
tcc_delete(state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment