You are looking at historical revision 13621 of this page. It may differ significantly from its current revision.

Introduction

A library for using the Tiny C compiler.

Examples

Compile and run a well known example:

(define s (tcc:new))
(tcc:set-output-type s tcc/output-memory)
(tcc:compile-string s "int main() { printf(\"Hello, world!\\n\"); return 0; }")
(tcc:run s '())
(tcc:delete s)

Compile a file with callbacks into Scheme:

(define-external (add (int a) (int b)) int
  (+ a b) )

(define my-program <<EOF
int fib(int n)
{
    if (n <= 2)
        return 1;
    else
        return fib(n-1) + fib(n-2);
}

int foo(int n)
{
    printf("Hello World!\n");
    printf("fib(%d) = %d\n", n, fib(n));
    printf("add(%d, %d) = %d\n", n, 2 * n, add(n, 2 * n));
    return 0;
}
EOF
)

(define call
  (foreign-callback-lambda* int ([c-pointer func] [int arg])
    "return(((int (*)(int))func)(arg));") )

(define s (tcc:new))
(tcc:set-output-type s tcc/output-memory)
(tcc:compile-string s my-program)
(tcc:add-symbol s "add" (location add))
(tcc:relocate s)
(define func (tcc:get-symbol s "foo"))
(print (call func 32))
(tcc:delete s)

Author

Felix Winkelmann

License

Copyright (c) 2000-2003, Felix L. Winkelmann All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICESLOSS OF USE, DATA, OR PROFITSOR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Requirements

libtcc must be installed on the host system.

Documentation

This extension is a thin wrapper around the libtcc library that allows compilation of C completely into memory, into an executable or a dynamically loadable library. The following descriptions are taken mostly from the libtcc.h header file.

[constant] tcc/output-memory
[constant] tcc/output-exe
[constant] tcc/output-dll
[constant] tcc/output-obj

Constants specifying the compilation target.

[procedure] (tcc:new)

Create a new TCC compilation context.

[procedure] (tcc:delete STATE)

Free a TCC compilation context.

[procedure] (tcc:enable-debug STATE)

Add debug information in the generated code.

[procedure] (tcc:set-warning STATE STRING BOOL)

Set error/warning display callback. If BOOL is true, then compiler errors and warnings raise an exception in the procedure invoking the compilation. The exception is a composite exception of the kinds exn and tcc.

[procedure] (tcc:add-include-path STATE STRING)

Add include path.

[procedure] (tcc:add-sysinclude-path STATE STRING)

Add in system include path.

[procedure] (tcc:define-symbol STATE SYMBOL VALUE)

Define preprocessor symbol SYMBOL as VALUE (both strings).

[procedure] (tcc:undefine-symbol STATE STRING)

Undefine preprocess symbol STRING.

[procedure] (tcc:add-file STATE STRING)

Add a file (either a C file, dll, an object, a library or an ld script).

[procedure] (tcc:compile-string STATE STRING)

Compile a string containing C source.

[procedure] (tcc:set-output-type STATE TYPE)

Set output type (like tcc:output/memory). Must be called before any compilation.

[procedure] (tcc:add-library-path STATE STRING)

Equivalent to -Lpath option.

[procedure] (tcc:add-library STATE STRING)

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

[procedure] (tcc:add-symbol STATE STRING POINTER)

Add a symbol to the compiled program.

[procedure] (tcc:output-file STATE STRING)

Output an executable, library or object file. Do not call tcc:relocate before.

[procedure] (tcc:run STATE ARGC ARGV)

Link and run main function and return its value. Do not call tcc:relocate before.

[procedure] (tcc:relocate STATE)

Do all relocations (needed before using tcc:get-symbol).

[procedure] (tcc:get-symbol STATE

Return symbol value as a pointer object.

Version History