Output type.
Add a file (C file, dll, object, library, ld script). Return -1 if error.
Add include path pathname.
The library name is the same as the argument of the '-l' option.
Equivalent to -Lpath option.
Add a symbol to the compiled program.
Add system include path pathname.
Compile source containing a C source. Return -1 if error.
Define preprocessor symbol named sym. Can put optional value.
Evaluate (Compile, link and run) source by running its main() and returning its value. DO NOT call relocate() before.
Return value of symbol named name or null if not found.
Output an executable, library or object file. DO NOT call relocate() before.
Do all relocations (needed before using get_symbol())
Link and run main() function and return its value. DO NOT call relocate() before.
Set error/warning display callback.
Set CONFIG_TCCDIR at runtime.
Set options opts as from command line (multiple supported). Important options are "-bt" "-b".
Set output type. MUST BE CALLED before any compilation.
Undefine preprocess symbol named sym.
Possible values for 'ptr': - RELOCATE_AUTO : Allocate and manage memory internally - NULL : return required memory size for the step below - memory address : copy code to memory passed by the caller Returns -1 if error.
TCC tcc; assert(tcc.compile(`int f(int x) { return x; }`) == 0);
TCC tcc; static extern(C) void error_func_ignore(void* opaque, const char* msg) {} tcc.setErrorFunction((void*).init, &error_func_ignore); assert(tcc.compile(`int f(int x) { return; }`) == 0); /+ TODO: check warning output +/ assert(tcc.compile(`int f(int x) { return }`) == -1); /+ TODO: check error output +/
Compile main().
TCC tcc; assert(tcc.compile(`int main(int argc, char** argv) { return 0; }`) == 0);
Run main().
version (linux) { TCC tcc; tcc.setOutputType(TCC.OutputType.exe); const src = `int main(int argc, char** argv) { return 42; }`; assert(tcc.eval(src, 0, null) == 42); }
Use stdio.h.
version (linux) { TCC tcc; tcc.setOutputType(TCC.OutputType.exe); const src = ` #include <stdio.h> int main(int argc, char** argv) { printf("Hello world!\n"); return 0; } `; assert(tcc.eval(src, 0, null) == 0); // assert(tcc.addLibraryPath(DirPath("/usr/lib/x86_64-linux-gnu/")) == 0); // assert(tcc.addLibrary("c") == 0); // C library }
Use gmp.h.
version (linux) { TCC tcc; tcc.setOutputType(TCC.OutputType.exe); const src = ` #include <gmp.h> int main(int argc, char** argv) { mpz_t x; mpz_init_set_ui(x, 42); const ret = mpz_get_ui(x); mpz_clear(x); return ret; } `; assert(tcc.addLibrary("gmp") == 0); // GNU MP assert(tcc.eval(src, 0, null) == 42); }
Set system include paths.
TCC tcc; tcc.addSystemIncludePath(DirPath(`/usr/include/`)); tcc.addSystemIncludePath(DirPath(`/usr/lib/x86_64-linux-gnu/tcc/`)); tcc.addSystemIncludePath(DirPath(`/usr/include/linux/`)); tcc.addSystemIncludePath(DirPath(`/usr/include/x86_64-linux-gnu/`));
In-memory compilation. See: https://gist.github.com/llandsmeer/3489603e5070ee8dbe75cdf1865a5ca9
TCC tcc; static extern(C) void error_func_ignore(void* opaque, const char* msg) @trusted { import core.stdc.string : strlen; import std.stdio; printf("opaque:%p\nmsg:\n%s\n", opaque, msg); } tcc.setErrorFunction((void*).init, &error_func_ignore); tcc.setOutputType(TCC.OutputType.memory); import std.datetime.stopwatch : StopWatch, AutoStart; auto sw = StopWatch(AutoStart.yes); static extern(C) int add1(int x) { return x+1; } tcc.addSymbol("add1", &add1); tcc.compile("int add1(int); int main() { return add1(41); }"); import std.stdio; writeln("tcc.compile took ", sw.peek); sw.reset(); const exitStatus = tcc.run(0, null); writeln("tcc.run took:", sw.peek); assert(exitStatus == 42);
Tiny C Compiler (TCC). See: https://briancallahan.net/blog/20220406.html