-
-
Save kybr/9f8374fef992b96148f2d4311249ec3d to your computer and use it in GitHub Desktop.
This file contains 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
⏿ c++11 recompile-a-function.cpp -ltcc ; and ./a.out | |
need 127 bytes. | |
relocating to 7fcefd403b90 | |
foo(0) = 0 | |
foo(1) = 1 | |
foo(2) = 2 | |
foo(3) = 3 | |
foo(4) = 4 | |
foo(5) = 5 | |
foo(6) = 6 | |
foo(7) = 7 | |
foo(8) = 8 | |
foo(9) = 9 | |
<string>: error: 'foo' defined twice | |
Assertion failed: (-1 != tcc_compile_string(instance, "char foo(int t) { return t; }")), function main, file recompile-a-function.cpp, line 24. | |
This file contains 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 <iostream> | |
using std::cout; | |
using std::endl; | |
#include "libtcc.h" | |
int main(int argc, char* argv[]) { | |
TCCState* instance = tcc_new(); | |
tcc_set_output_type(instance, TCC_OUTPUT_MEMORY); | |
assert(-1 != tcc_compile_string(instance, "char foo(int t) { return t; }")); | |
int size; | |
assert(-1 != (size = tcc_relocate(instance, NULL))); | |
char* memory = new char[size]; | |
printf("need %d bytes.\n", size); | |
printf("relocating to %llx\n", (unsigned long long)memory); | |
assert(-1 != tcc_relocate(instance, (void*)memory)); | |
// tcc_delete(instance); // uncomment this line to crash on macOS | |
// | |
// deleting the tcc instance here on macOS leads to a crash later, | |
// then foo is called. | |
// | |
char (*foo)(int) = (char (*)(int))tcc_get_symbol(instance, "foo"); | |
for (int t = 0; t < 10; t++) { | |
printf("foo(%d) = %d\n", foo(t), t); | |
} | |
// even after a relocate, we may not recompile foo | |
// <string>: error: 'foo' defined twice | |
assert(-1 != tcc_compile_string(instance, "char foo(int t) { return t; }")); | |
tcc_delete(instance); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment