LinuxCommandLibrary

tcc

Compile and run C code quickly

TLDR

Compile and link 2 source files to generate an executable

$ tcc -o [executable_name] [path/to/file1.c] [path/to/file2.c]
copy

Directly run an input file like a script and pass arguments to it
$ tcc -run [path/to/source_file.c] [arguments]
copy

Interpret C source files with a shebang inside the file
$ #!/[path/to/tcc] -run
copy

SYNOPSIS

tcc is invoked from the command line, typically followed by options and input files.

Compilation and Linking:

tcc [options] files... [-o outputfile]

Running C Scripts Directly:
tcc -run [options] c_script_file [script_arguments...]

Creating Shared Libraries:
tcc -shared [options] files... [-o output_library.so]

PARAMETERS

-c
    Compile source files only, without linking. Produces object files (.o).

-o file
    Specify the name of the output file (executable, library, or object file).

-I dir
    Add dir to the list of directories to search for include files.

-L dir
    Add dir to the list of directories to search for libraries.

-l lib
    Link with the specified library lib. (e.g., -lm for math library).

-D macro[=val]
    Define a preprocessor macro with an optional value.

-U macro
    Undefine a preprocessor macro.

-g
    Generate debugging information (DWARF).

-run
    Compile and execute the first input file directly as a C script. Subsequent arguments are passed to the script.

-shared
    Generate a shared library (.so) instead of an executable.

-static
    Link all libraries statically, producing a self-contained executable.

-Wall
    Enable all common compiler warnings.

-Werror
    Treat all warnings as errors.

-std=std
    Specify the C standard to use (e.g., c99, c11). TCC primarily supports C99.

-v
    Display the compiler version and other verbose information.

DESCRIPTION

The Tiny C Compiler (TCC) is an extremely small, fast, and feature-rich C compiler designed by Fabrice Bellard. Unlike larger compilers like GCC or Clang, TCC emphasizes speed and a small memory footprint, making it ideal for rapid prototyping, embedded systems, and situations where compilation speed is paramount.

It supports the C99 standard and provides several unique features, including the ability to compile C code directly into machine code for immediate execution (Just-In-Time compilation) and compile C scripts directly, similar to how shell scripts are executed. TCC can output executables, shared libraries, or assembly files. While it may not produce code as highly optimized as GCC for complex applications, its rapid compilation times and compact size make it a valuable tool for specific use cases, including system bootstrapping, educational purposes, and lightweight development environments.

CAVEATS

While powerful for its size, TCC has some limitations compared to full-featured compilers like GCC or Clang. It provides less aggressive code optimization, which might lead to slightly less performant binaries for complex applications. Its support for newer C standards (beyond C99) and certain exotic features may also be limited or incomplete. TCC might not be pre-installed on all Linux distributions.

JIT COMPILATION AND SCRIPTING

One of TCC's most notable features is its built-in Just-In-Time (JIT) compiler. This allows TCC to compile C code into machine code in memory and execute it immediately, without creating intermediate object files or executables on disk. This capability, combined with the -run option, enables TCC to act as a powerful C script interpreter. You can add #!/usr/bin/tcc -run as the shebang line to a C source file, making it directly executable like any other script.

HISTORY

TCC was originally developed by Fabrice Bellard, a renowned French computer programmer known for his many innovative projects. The first public release was around 2002. Bellard created TCC with the goal of producing an extremely small and fast C compiler, capable of compiling itself and being used in scenarios where resource constraints or rapid compilation were critical. Its unique JIT compilation capabilities and ability to execute C files directly as scripts distinguish it from other compilers. It has found niches in embedded systems, booting environments, and as a lightweight alternative for educational purposes or quick experiments.

SEE ALSO

gcc(1), clang(1), make(1), ld(1), ar(1)

Copied to clipboard