lazylib

Types

LazyLibNotFound = object of LazyException
LazySymNotFound = object of LazyException

Vars

lazyLibs = new(Lazylibs)

Procs

proc lazyLoadLib(name: string) {...}{.raises: [Exception, LazyLibNotFound],
                              tags: [RootEffect].}

Load specified library name at runtime - used by {.lazylib.}, no need to directly call this proc.

name can be a library name, full path or pattern as supported by {.dynlib.}

If library is not found, raise LazyLibNotFound which can be caught and handled as required by app.

proc lazySymAddr(name, symName: string): pointer {...}{.
    raises: [Exception, LazyLibNotFound, KeyError, LazySymNotFound],
    tags: [RootEffect].}

Load specified symName from library name at runtime - used by {.lazylib.}, no need to directly call this proc.

name can be a library name, full path or pattern as supported by {.dynlib.}. lazyLoadSym() will load the lib if not already done with lazyLoadLib().

symName is the name of the symbol to load.

If library is not found, raise LazyLibNotFound which can be caught and handled as required by app. If symbol is not found, raise LazySymNotFound.

Returns pointer to loaded symbol.

Macros

macro lazylib(name, procDef: untyped): untyped

Pragma to load C/C++ libraries and symbols at runtime

Drop-in substitute for {.dynlib.}, difference being that the library and symbol are loaded on use rather than at app startup. This allows the developer to handle error conditions like a missing library or symbol gracefully.

Raises LazyLibNotFound if library is not found and LazySymNotFound if symbol is not found in the loaded library.

Specify calling convention like {.cdecl.} per usual as well as {.importc.} if there's a need to modify or shorten the C name.

Example:

const
  lib = "libz.so"

proc Version*(): cstring {.lazylib: lib, cdecl, importc: "zlib$1".}

echo Version()