~ chicken-core (chicken-5) /manual/Units and linking model
Trap1[[tags: manual]]2[[toc:]]34== Units and the linking model56Compiling Scheme code to standalone executables or dynamically7loadable files is the most common and simplest way of using CHICKEN.8The extension system handles straightforward cases of static linking9of modules in a mostly transparent way, which is usually sufficient10for normal situations.1112But for more advanced uses like static linking, creating dynamic13libraries or embedding compiled code into other (usually C/C++ based)14applications it is helpful to understand the internal model CHICKEN15uses to organize compiled code and separate compilation units.1617Every compiled Scheme file (from here on called a ''compilation unit'')18consists of a ''toplevel'' C function holding the compiled toplevel19expressions in the order in which they appear in the source file. Scheme20functions ({{lambda}}s) are compiled 1-to-1 into additional C functions,21including the intermediate lambda-functions that are the result of the CPS22conversion that is done by the compiler.2324The toplevel C function of a compilation unit is comparable to the25{{main}} function in C programs, and for standalone executables26the startup code inside the runtime system will eventually call this27toplevel function. Dynamically loaded compiled code is structured28in the same way, with a toplevel function that is dynamically looked29up in the loaded binary and invoked to execute the expressions from30the loaded code. Statically linked compilation units are treated similarly,31there also exists a toplevel function which is called at some stage in the32startup process to execute the forms of the file.3334For standalone executables and dynamically loaded code the toplevel35function has a fixed, predefined name ({{C_toplevel}}). For static36linking or for using multiple toplevels in a shared library that combines37multiple compilation units (like {{libchicken}}, for example), non-internal38function names have to be different to be properly39distinguished, so we assign a unique ''unit'' name to each compilation unit that40is intended to be linked with other compilation units.4142To set the name of a compilation unit, use4344<enscript highlight=scheme>45(declare (unit UNITNAME))46</enscript>4748''Invocation'' of a unit (actually running the toplevel code contained in it) is done49automatically for standalone programs and dynamically loaded compiled code,50but must be done explicitly for uniquely named units that are part of a larger51library or when doing static linking. To do so, use5253<enscript highlight=scheme>54(declare (uses UNITNAME))55</enscript>5657Invocation takes place at the start of the current compilation unit, so the58toplevel of any ''used'' units is executed before the toplevel of the compilation59unit that is ''using'' one. Invocation can also be done explicitly by using60{{load-library}} (from the {{(chicken load)}}) module,61which takes the name of a unit to be invoked as an62argument.6364Note that this model of using code from other compilation units does not65address syntax definitions, it's for running pure, fully expanded and compiled66code. Syntax and modules are handled at a higher level, using import67libraries, which are compiled or interpreted separate files setting up module68information to allow the compiler to properly resolve module namespaces69and imports.7071----72Previous: [[Egg specification format]]7374Next: [[Deployment]]