Tcl_InitStubs(3tcl)

✨ NAME ✨

okay so this is the page for Tcl_InitStubs from the Tcl Library Procedures manual (that's the Tcl_InitStubs(3tcl) one, section 3, iykyk) β€” and it does exactly one iconic thing: initialize the Tcl stubs mechanism πŸ’…


🌸 SYNOPSIS 🌸

#include <tcl.h>

const char *
Tcl_InitStubs(interp, version, exact)

πŸŽ€ ARGUMENTS πŸŽ€

  • Tcl_Interp *interp (in) β€” the Tcl interpreter handle, bestie. your lil portal to the interpreter.
  • const char *version (in) β€” a version string saying which minimal version of Tcl you're willing to accept. normally you just write "9.0". or "8.6-" if you're accepting both 8.6 and 9.0, no cap.
  • int exact (in) β€” 1 means only that exact particular version specified by version is accepted. 0 means versions newer than version are also accepted. and if version ends with -, higher major versions get accepted too; otherwise the major version has to be the same as in version. other bits have no effect, they're just vibes.

πŸ¦‹ INTRODUCTION πŸ¦‹

so the Tcl stubs mechanism defines a way to dynamically bind extensions to a particular Tcl implementation at run time. and it gives Tcl users two genuinely significant benefits:

  1. Extensions that use the stubs mechanism can be loaded into multiple versions of Tcl without being recompiled or relinked, as long as the major Tcl version is the same. periodt. πŸ’…
  2. Extensions that use the stubs mechanism can be dynamically loaded into statically-linked Tcl applications.

how does it pull this off? the stubs mechanism exports function tables that define an interface to the Tcl API. then the extension accesses the Tcl API through offsets into that function table, so there are no direct references to any of the Tcl library's symbols. and the whole redirection is transparent to the extension, so an extension writer can keep on using all public Tcl functions as documented. it's giving magic, but make it ABI πŸͺ„

the stubs mechanism requires no changes to applications incorporating Tcl interpreters. only developers creating C-based Tcl extensions need to take steps to use the stubs mechanism with their extensions.

enabling the stubs mechanism for an extension requires these steps:

  1. Call Tcl_InitStubs in the extension before calling any other Tcl functions.
  2. Define the USE_TCL_STUBS symbol. typically you'd include the -DUSE_TCL_STUBS flag when compiling the extension.
  3. Link the extension with the Tcl stubs library instead of the standard Tcl library. for example, to use the Tcl 9.0 ABI on Unix platforms, the library name is libtclstub.a; on Windows platforms, the library name is tclstub.lib.

if the extension also requires the Tk API, it must also call Tk_InitStubs to initialize the Tk stubs interface and link with the Tk stubs libraries. see the Tk_InitStubs page for more info, bestie 🫢


πŸ‘‘ DESCRIPTION πŸ‘‘

Tcl_InitStubs attempts to initialize the stub table pointers and make sure the correct version of Tcl is loaded. besides an interpreter handle, it accepts as arguments a version number and a Boolean flag saying whether the extension requires an exact version match or not. if exact is 0, then versions newer than version are also accepted. if version ends with -, higher major versions are accepted as well; otherwise the major version must be the same as in version. 1 means only the specified version is accepted. version can be any construct as described for package require (that's the PACKAGE manual page, in the section REQUIREMENT). multiple requirement strings, like with package require, are not supported. 🚫

Tcl_InitStubs returns a string containing the actual version of Tcl satisfying the request, or NULL if the Tcl version is not accepted, does not support stubs, or any other error condition occurred. 😭

fr fr: NULL is not a personality trait, it's your error signal. handle it.

🌈 SEE ALSO 🌈

  • Tk_InitStubs
  • package

⭐ KEYWORDS ⭐

  • stubs

and that's the whole page, bestie πŸ’– this one's from the Tcl manual itself β€” Tcl version 8.1, dated 8.1, filed under Tcl_InitStubs(3tcl). go forth and initialize those stubs, you absolute icon πŸ‘‘βœ¨

πŸ“¦ where this page comes from