Recent versions of luatex (≥ 1.0.3) come with ffi. That means that one can call a C library from lua inside a TeX file. Some (very) recent examples on TeX.sx have proven its power.

Assuming current TeXlive (2018) or next release (TeXlive 2019), what libraries can be accessed with no additional installation from the user (from a package writer point of vue)?


Without additional installation only the symbols from LuaTeX itself and its dynamically linked libraries are accessible. However, the interesting question is which symbols are accessible which I will show in the following.

Finding symbols

The tools nm and ldd I use below should be available on any POSIX system. On Windows you'd use dumpbin.exe tool for these jobs.

You can find a list of symbols using the nm tool. The grep ' T ' filters only symbols in the text section of the executable (i.e. only functions are shown). Because in TeX Live 2018 there is some C++ code linked into LuaTeX you have to use the --demangle option to get something human readable.

$ nm -D --demangle `which luatex` | grep ' T '
[...huge list...]

I'm not sure how useful this is. I have never called a function from the LuaTeX executable.

To find the dynamically linked libraries you can use the ldd tool.

$ ldd `which luatex`
    linux-vdso.so.1 (0x00007ffe4d118000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007faa4ba0d000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007faa4b66f000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faa4b27e000)
    /lib64/ld-linux-x86-64.so.2 (0x00007faa4bc11000)

The symbols defined by these libraries can be looked up using the nm tool again but it's better to look in the corresponding documentation. The libraries most useful to you are probably libm.so.6 which is the C mathematical functions library (reference) and libc.so.6 which is the C standard library (reference). On POSIX systems this includes the full POSIX C library (reference).

When using the FFI all these symbols are available through the ffi.C handle without loading a library using ffi.load.

Cross-platform FFI code

If you restrict the set of functions you call via FFI to the C standard library the resulting code should be portable between POSIX systems and Windows.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.