Skip to main content
Before you can compile code with libtcc, you need to create and configure a compilation context (TCCState).

Creating a compilation context

tcc_new

Creates a new TCC compilation context.
TCCState*
Pointer to the newly created compilation context, or NULL if allocation fails.
Example:
You must call tcc_set_output_type() before any compilation.

Destroying a compilation context

tcc_delete

Frees a TCC compilation context and all associated resources.
TCCState*
required
The compilation context to free.
Example:

Setting output type

tcc_set_output_type

Sets the output type for the compilation. Must be called before any compilation.
TCCState*
required
The compilation context.
int
required
The type of output to generate. One of:
  • TCC_OUTPUT_MEMORY (1) - Run code in memory
  • TCC_OUTPUT_EXE (2) - Executable file
  • TCC_OUTPUT_DLL (4) - Dynamic library
  • TCC_OUTPUT_OBJ (3) - Object file
  • TCC_OUTPUT_PREPROCESS (5) - Only preprocess
int
Returns 0 on success, -1 on error.
Example:

Configuring paths

tcc_set_lib_path

Sets the path where TCC will look for its internal libraries and include files.
TCCState*
required
The compilation context.
const char*
required
Path to the TCC library directory (typically contains include/ and lib/).
Example:

tcc_add_include_path

Adds a directory to the include file search path.
TCCState*
required
The compilation context.
const char*
required
Path to add to the include search path.
int
Returns 0 on success.
Example:

tcc_add_sysinclude_path

Adds a system include path to the default system include paths.
TCCState*
required
The compilation context.
const char*
required
System include path to add.
int
Returns 0 on success.

Setting options

tcc_set_options

Sets compiler options as if passed on the command line. Multiple calls are supported.
TCCState*
required
The compilation context.
const char*
required
Command-line style options (e.g., “-Wall -O2”).
int
Returns 0 on success, -1 on error.
Example:

Error handling

tcc_set_error_func

Sets a custom error and warning callback.
TCCState*
required
The compilation context.
void*
User data pointer passed to the error callback (can be NULL).
TCCErrorFunc*
Callback function for errors and warnings. If NULL, errors are printed to stderr.
Example:

Preprocessor configuration

tcc_define_symbol

Defines a preprocessor symbol.
TCCState*
required
The compilation context.
const char*
required
Symbol name. Can be “sym=val” form, or just “sym”.
const char*
Symbol value. If NULL and sym doesn’t contain ’=’, defaults to “1”.
Example:

tcc_undefine_symbol

Undefines a preprocessor symbol.
TCCState*
required
The compilation context.
const char*
required
Symbol name to undefine.
Example:

Custom memory allocator

tcc_set_realloc

Sets a custom memory allocator for all TCC allocations. This is a global setting.
TCCReallocFunc*
Custom realloc-style function. If NULL, uses the default allocator. The function should free memory when size is 0.
This must be called before creating any TCCState instances, as it affects all TCC memory allocation globally.
Example:

Complete initialization example