source: project/wiki/tcc @ 8833

Last change on this file since 8833 was 8833, checked in by Alaric Snell-Pym, 12 years ago

FIrst draft of tcc egg wikification

File size: 5.4 KB
Line 
1[[tags: egg]
2
3== Introduction
4
5A library for using the [[http://www.tinycc.org|Tiny C compiler]].
6
7== Examples
8
9Compile and run a well known example:
10
11<enscript highlight="scheme">
12(define s (tcc:new))
13(tcc:set-output-type s tcc/output-memory)
14(tcc:compile-string s "int main() { printf(\"Hello, world!\\n\"); return 0; }")
15(tcc:run s '())
16(tcc:delete s)
17</enscript>
18
19Compile a file with callbacks into Scheme:
20
21<enscript highlight="scheme">
22(define-external (add (int a) (int b)) int
23  (+ a b) )
24
25(define my-program <<EOF
26int fib(int n)
27{
28    if (n <= 2)
29        return 1;
30    else
31        return fib(n-1) + fib(n-2);
32}
33
34int foo(int n)
35{
36    printf("Hello World!\n");
37    printf("fib(%d) = %d\n", n, fib(n));
38    printf("add(%d, %d) = %d\n", n, 2 * n, add(n, 2 * n));
39    return 0;
40}
41EOF
42)
43
44(define call
45  (foreign-callback-lambda* int ([c-pointer func] [int arg])
46    "return(((int (*)(int))func)(arg));") )
47
48(define s (tcc:new))
49(tcc:set-output-type s tcc/output-memory)
50(tcc:compile-string s my-program)
51(tcc:add-symbol s "add" (location add))
52(tcc:relocate s)
53(define func (tcc:get-symbol s "foo"))
54(print (call func 32))
55(tcc:delete s)
56</enscript>
57
58== Author
59
60[[felix winkelmann|Felix Winkelmann]]
61
62== License
63
64Copyright (c) 2000-2003, Felix L. Winkelmann
65All rights reserved.
66
67Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
68conditions are met:
69
70  Redistributions of source code must retain the above copyright notice, this list of conditions and the following
71    disclaimer.
72  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
73    disclaimer in the documentation and/or other materials provided with the distribution.
74  Neither the name of the author nor the names of its contributors may be used to endorse or promote
75    products derived from this software without specific prior written permission.
76
77THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
78OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
79AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
80CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
81CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82SERVICESLOSS OF USE, DATA, OR PROFITSOR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
83THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
84OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
85POSSIBILITY OF SUCH DAMAGE.
86
87== Requirements
88
89{{libtcc}} must be installed on the host system.
90
91== Documentation
92
93This extension is a thin wrapper around the {{libtcc}} library that allows compilation of
94C completely into memory, into an executable or a dynamically loadable library. The following descriptions
95are taken mostly from the {{libtcc.h}} header file.
96
97<constant>tcc/output-memory</constant>
98<constant>tcc/output-exe</constant>
99<constant>tcc/output-dll</constant>
100<constant>tcc/output-obj</constant>
101
102Constants specifying the compilation target.
103
104<procedure>(tcc:new)</procedure>
105
106Create a new TCC compilation context.
107
108<procedure>(tcc:delete STATE)</procedure>
109
110Free a TCC compilation context.
111
112<procedure>(tcc:enable-debug STATE)</procedure>
113
114Add debug information in the generated code.
115
116<procedure>(tcc:set-warning STATE STRING BOOL)</procedure>
117
118Set error/warning display callback. If {{BOOL}} is true, then compiler errors and warnings
119raise an exception in the procedure invoking the compilation. The exception is a composite exception of the
120kinds {{exn}} and {{tcc}}.
121
122<procedure>(tcc:add-include-path STATE STRING)</procedure>
123
124Add include path.
125
126<procedure>(tcc:add-sysinclude-path STATE STRING)</procedure>
127
128Add in system include path.
129
130<procedure>(tcc:define-symbol STATE SYMBOL VALUE)</procedure>
131
132Define preprocessor symbol {{SYMBOL}} as {{VALUE}} (both strings).
133
134<procedure>(tcc:undefine-symbol STATE STRING)</procedure>
135
136Undefine preprocess symbol {{STRING}}.
137
138<procedure>(tcc:add-file STATE STRING)</procedure>
139
140Add a file (either a C file, dll, an object, a library or an ld script).
141
142<procedure>(tcc:compile-string STATE STRING)</procedure>
143
144Compile a string containing C source.
145
146<procedure>(tcc:set-output-type STATE TYPE)</procedure>
147
148Set output type (like {{tcc:output/memory}}). '''Must be called''' before any compilation.
149
150<procedure>(tcc:add-library-path STATE STRING)</procedure>
151
152Equivalent to {{-L}}''{{path}}'' option.
153
154<procedure>(tcc:add-library STATE STRING)</procedure>
155
156The library name is the same as the argument of the {{-l}} option.
157
158<procedure>(tcc:add-symbol STATE STRING POINTER)</procedure>
159
160Add a symbol to the compiled program.
161
162<procedure>(tcc:output-file STATE STRING)</procedure>
163
164Output an executable, library or object file. '''Do not''' call
165{{tcc:relocate}} before.
166
167<procedure>(tcc:run STATE ARGC ARGV)</procedure>
168
169Link and run {{main}} function and return its value. '''Do not''' call {{tcc:relocate}} before.
170
171<procedure>(tcc:relocate STATE)</procedure>
172
173Do all relocations (needed before using {{tcc:get-symbol}}).
174
175<procedure>(tcc:get-symbol STATE</procedure>
176
177Return symbol value as a pointer object.
178
179== Version History
180
181* 1.3: renamed deprecated use of {{foreign-callback-lambda}}.
182* 1.2: {{tcc:get-symbol}} was fixed to compile with newest tcc version [Thanks to Damian M. Gryski]
183* 1.1: Adapted to new setup scheme.
184* 1.0
Note: See TracBrowser for help on using the repository browser.