TCC

Destructor

~this
~this()
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

ErrorFunc
alias ErrorFunc = void function(void* opaque, const char* msg)
Undocumented in source.
compileAndRun
alias compileAndRun = eval
Undocumented in source.

Enums

OutputType
enum OutputType

Output type.

Functions

addFile
int addFile(char[] filename)
int addFile(FilePath path)

Add a file (C file, dll, object, library, ld script). Return -1 if error.

addIncludePath
int addIncludePath(char[] pathname)
int addIncludePath(DirPath path)

Add include path pathname.

addLibrary
int addLibrary(char[] libraryname)

The library name is the same as the argument of the '-l' option.

addLibraryPath
int addLibraryPath(char[] pathname)

Equivalent to -Lpath option.

addLibraryPath
int addLibraryPath(DirPath path)
Undocumented in source. Be warned that the author may not have intended to support it.
addSymbol
int addSymbol(char[] name, void* val)

Add a symbol to the compiled program.

addSystemIncludePath
int addSystemIncludePath(char[] pathname)
int addSystemIncludePath(DirPath path)

Add system include path pathname.

compile
int compile(char[] source)

Compile source containing a C source. Return -1 if error.

defineSymbol
void defineSymbol(char[] sym, char[] value)

Define preprocessor symbol named sym. Can put optional value.

eval
int eval(char[] source, int argc, char** argv)

Evaluate (Compile, link and run) source by running its main() and returning its value. DO NOT call relocate() before.

getSymbolMaybe
void* getSymbolMaybe(char[] name)

Return value of symbol named name or null if not found.

outputFile
int outputFile(char[] filename)
int outputFile(FilePath filename)

Output an executable, library or object file. DO NOT call relocate() before.

relocate
int relocate(void* ptr)

Do all relocations (needed before using get_symbol())

run
int run(int argc, char** argv)

Link and run main() function and return its value. DO NOT call relocate() before.

setErrorFunction
void setErrorFunction(void* error_opaque, ErrorFunc error_func)

Set error/warning display callback.

setLibraryPath
void setLibraryPath(char[] path)
void setLibraryPath(DirPath path)

Set CONFIG_TCCDIR at runtime.

setOptions
int setOptions(char[] opts)

Set options opts as from command line (multiple supported). Important options are "-bt" "-b".

setOutputType
int setOutputType(OutputType output_type)

Set output type. MUST BE CALLED before any compilation.

undefineSymbol
void undefineSymbol(char[] sym)

Undefine preprocess symbol named sym.

Manifest constants

RELOCATE_AUTO
enum RELOCATE_AUTO;

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.

Variables

_state
TCCState* _state;
Undocumented in source.

Examples

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/`));
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);

Meta