Note: This is taken from the Chicken Wiki, where a more recent version could be available.
A library for using the Tiny C compiler.
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)
Copyright © 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.
libtcc must be installed on the host system.
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> <constant>tcc/output-exe</constant> <constant>tcc/output-dll</constant> <constant>tcc/output-obj</constant>
Constants specifying the compilation target.
<procedure>(tcc:new)</procedure>
Create a new TCC compilation context.
<procedure>(tcc:delete STATE)</procedure>
Free a TCC compilation context.
<procedure>(tcc:enable-debug STATE)</procedure>
Add debug information in the generated code.
<procedure>(tcc:set-warning STATE STRING BOOL)</procedure>
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)</procedure>
Add include path.
<procedure>(tcc:add-sysinclude-path STATE STRING)</procedure>
Add in system include path.
<procedure>(tcc:define-symbol STATE SYMBOL VALUE)</procedure>
Define preprocessor symbol SYMBOL as VALUE (both strings).
<procedure>(tcc:undefine-symbol STATE STRING)</procedure>
Undefine preprocess symbol STRING.
<procedure>(tcc:add-file STATE STRING)</procedure>
Add a file (either a C file, dll, an object, a library or an ld script).
<procedure>(tcc:compile-string STATE STRING)</procedure>
Compile a string containing C source.
<procedure>(tcc:set-output-type STATE TYPE)</procedure>
Set output type (like tcc:output/memory). Must be called before any compilation.
<procedure>(tcc:add-library-path STATE STRING)</procedure>
Equivalent to -Lpath option.
<procedure>(tcc:add-library STATE STRING)</procedure>
The library name is the same as the argument of the -l option.
<procedure>(tcc:add-symbol STATE STRING POINTER)</procedure>
Add a symbol to the compiled program.
<procedure>(tcc:output-file STATE STRING)</procedure>
Output an executable, library or object file. Do not call tcc:relocate before.
<procedure>(tcc:run STATE ARGC ARGV)</procedure>
Link and run main function and return its value. Do not call tcc:relocate before.
<procedure>(tcc:relocate STATE)</procedure>
Do all relocations (needed before using tcc:get-symbol).
<procedure>(tcc:get-symbol STATE</procedure>
Return symbol value as a pointer object.