# luatex cannot find dynamic shared library with FFI

After a while, I'd like to play again with the FFI library with the help of an answer and a tutorial written by Henri Menke.

I run an archlinux up-to-date system with a TeXLive 2021 vanilla installed and up-to-date.

Unfortunately, running the following minimal example:

\documentclass{minimal}

\usepackage{luacode}

\begin{luacode*}
local ffi = require("ffi")
local gsl = ffi.load("gsl")
\end{luacode*}

\begin{document}

\end{document}


fails with:

christophe@cesium /tmp % lualatex --shell-escape t.tex
This is LuaHBTeX, Version 1.13.2 (TeX Live 2021)
system commands enabled.
(./t.tex
LaTeX2e <2021-11-15>
L3 programming layer <2021-11-22>
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/minimal.cls
Document Class: minimal 2001/05/25 Standard LaTeX minimal class
) (/usr/local/texlive/2021/texmf-dist/tex/lualatex/luacode/luacode.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/iftex/ifluatex.sty
(/usr/local/texlive/2021/texmf-dist/tex/generic/iftex/iftex.sty))
(/usr/local/texlive/2021/texmf-dist/tex/luatex/luatexbase/luatexbase.sty
(/usr/local/texlive/2021/texmf-dist/tex/luatex/ctablestack/ctablestack.sty)))[\
directlua]:2: could not load library gsl
stack traceback:
[C]: in function 'FFISUPPORTED'
[\directlua]:2: in main chunk.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.8 \end{luacode*}


My system has:

/usr/lib/libgsl.so
/usr/lib/libgsl.so.25
/usr/lib/libgsl.so.25.1.0
/usr/lib/libgslcblas.so
/usr/lib/libgslcblas.so.0
/usr/lib/libgslcblas.so.0.0.0


I tried hardcoding the path, like local gsl = ffi.load("/usr/lib/libgsl.so"), but I still get could not load library /usr/lib/libgsl.so.

The luatex manual has no hint : it doesn't even mention the ffi library. Should I use luaJITtex instead? I'm quite confused to be honest.

Note that luajit is able to find the gsl library but fails too:

christophe@cesium /tmp % luajit -v
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
christophe@cesium /tmp % cat t.lua
local ffi = require("ffi")
local gsl = ffi.load("gsl")
christophe@cesium /tmp % luajit t.lua
luajit: t.lua:2: /usr/lib/libgsl.so: undefined symbol: cblas_ctrmv


Any ideas?

The documentation for GSL states in the section about using the shared library:

To link against the library you need to specify both the main library and a supporting CBLAS library, which provides standard basic linear algebra subroutines. A suitable CBLAS implementation is provided in the library libgslcblas.a if your system does not provide one.

So you have to link to a CBLAS library too in order to use GSL. (This isn't done automatically since you might want use a different CBLAS library depending on your system) You can do this by running ffi.load for you chosen CBLAS implementation before loading gsl, e.g. (for GSL's bundled cblas):

\documentclass{article}
\usepackage{luacode}

\begin{luacode*}
ffi.load("gslcblas") -- Replace with another cblas for better performance
local gsl = ffi.load("gsl")
\end{luacode*}

\begin{document}
\end{document}

• Thanks (+1) and accepted. Do you know if there is some documentation for the ffi library in luatex? Nov 30 '21 at 14:35
• So it was the same issue as with luajit but obfuscated but luatex I guess? Nov 30 '21 at 14:37
• @cjorssen It should be mostly compatible with the ffi library in LuaJIT, so you can use the documentation there. The repo of the used library is github.com/facebookarchive/luaffifb, but that mostly links to the LuaJIT docs. As you've seen, error messages can be slightly different and there's also some difference regarding NULL. Nov 30 '21 at 14:59