Skip to main content
After compilation, you typically need to link against libraries and add external symbols.

Adding libraries

tcc_add_library

Links a library to the compiled code. The library name follows the convention used by the -l linker option.
TCCState*
required
The compilation context.
const char*
required
Library name without the “lib” prefix or file extension. For special cases, prefix with : to use an exact filename.
int
Returns 0 on success, -1 on error.
Example:
The function searches for libraries in this order:
  1. Dynamic libraries (.so, .dylib, or .dll depending on platform)
  2. Static libraries (.a)

tcc_add_library_path

Adds a directory to the library search path (equivalent to -L option).
TCCState*
required
The compilation context.
const char*
required
Directory path to add to the library search path.
int
Returns 0 on success.
Example:

Adding symbols

tcc_add_symbol

Adds a symbol (function or variable) to the compiled program, making host application symbols available to compiled code.
TCCState*
required
The compilation context.
const char*
required
Symbol name (must match the name used in compiled code).
const void*
required
Pointer to the function or variable.
int
Returns 0 on success, -1 on error.
Example:
You must add symbols before calling tcc_relocate(). Symbols cannot be added after relocation.

Output file generation

tcc_output_file

Generates an output file (executable, library, or object). Do not call tcc_relocate() before this.
TCCState*
required
The compilation context.
const char*
required
Path for the output file.
int
Returns 0 on success, -1 on error.
Example:

Complete linking example

Platform-specific notes

Windows (PE format)

On Windows, dynamically linked data needs the dllimport attribute:

Library search order

Libraries are searched in this order:
  1. Paths specified with tcc_add_library_path()
  2. Default system paths (/usr/local/lib, /usr/lib, /lib)
  3. Paths from LIBRARY_PATH environment variable