lib

A lib declaration groups C functions and types that belong to a library.

  1. @[Link("pcre")]
  2. lib LibPCRE
  3. end

Although not enforced by the compiler, a lib‘s name usually starts with Lib.

Attributes are used to pass flags to the linker to find external libraries:

  • @[Link("pcre")] will pass -lpcre to the linker, but the compiler will first try to use pkg-config.
  • @[Link(ldflags: "...")] will pass those flags directly to the linker, without modification. For example: @[Link(ldflags: "-lpcre")]. A common technique is to use backticks to execute commands: @[Link(ldflags: "`pkg-config libpcre --libs`")].
  • @[Link(framework: "Cocoa")] will pass -framework Cocoa to the linker (only useful in macOS).

Attributes can be omitted if the library is implicitly linked, as in the case of libc.

Reflection

Lib functions are visible in the macro language anywhere in the program using the method TypeNode#methods:

  1. lib LibFoo
  2. fun foo
  3. end
  4. {{ LibFoo.methods }} # => [fun foo]