Dynlib pragma for import

With the dynlib pragma a procedure or a variable can be imported from a dynamic library (.dll files for Windows, lib*.so files for UNIX). The non-optional argument has to be the name of the dynamic library:

  1. proc gtk_image_new(): PGtkWidget
  2. {.cdecl, dynlib: "libgtk-x11-2.0.so", importc.}

In general, importing a dynamic library does not require any special linker options or linking with import libraries. This also implies that no devel packages need to be installed.

The dynlib import mechanism supports a versioning scheme:

  1. proc Tcl_Eval(interp: pTcl_Interp, script: cstring): int {.cdecl,
  2. importc, dynlib: "libtcl(|8.5|8.4|8.3).so.(1|0)".}

At runtime the dynamic library is searched for (in this order):

  1. libtcl.so.1
  2. libtcl.so.0
  3. libtcl8.5.so.1
  4. libtcl8.5.so.0
  5. libtcl8.4.so.1
  6. libtcl8.4.so.0
  7. libtcl8.3.so.1
  8. libtcl8.3.so.0

The dynlib pragma supports not only constant strings as argument but also string expressions in general:

  1. import os
  2. proc getDllName: string =
  3. result = "mylib.dll"
  4. if fileExists(result): return
  5. result = "mylib2.dll"
  6. if fileExists(result): return
  7. quit("could not load dynamic library")
  8. proc myImport(s: cstring) {.cdecl, importc, dynlib: getDllName().}

Note: Patterns like libtcl(|8.5|8.4).so are only supported in constant strings, because they are precompiled.

Note: Passing variables to the dynlib pragma will fail at runtime because of order of initialization problems.

Note: A dynlib import can be overridden with the --dynlibOverride:name command line option. The Compiler User Guide contains further information.