Search code examples
luacompilationluajit

C library build for LuaJit


Please, help me to understand the build process for luajit.

If I want to compile a c library I use this code:

gcc -shared -fpic -O -I/path-to-luajit-headers/luajit-2.1  mylib.c -o mylib.so

Is it needed to use more specific parameters like this:

gcc -shared -fpic -O -I/path-to-luajit-headers/luajit-2.1 -L/path-to-luajit/lib mylib.c -o mylib.so -lluajit-2.1

In the second case the .so file is twice larger than the first one. What is the difference? Is it important to use -lluajit-2.1 , etc. May be there are more parameters to define luajit mode instead of pure lua building mode?


Solution

  • The interpreter needs access to the Lua API, and C extension modules need access to the Lua API, and both need to link to the same library, because otherwise bad things will happen (e.g. there will be two sets of static variables).

    There are two common approaches to achieve theses requirements.

    1. Compile the Lua API as a shared library/DLL and link both the interpreter and the extension modules to this shared library. This is the approach that is taken on Windows.
    2. Compile the Lua API as a static library and link it to the interpreter, but make sure that the interpreter re-exports the Lua API. This way the extension modules have access when they are loaded. This is the approach usually taken on Unix platforms. It requires the -Wl,-E linker flag when building the interpreter.

    Since you seem to run a Unixoid OS, your first approach is probably the correct one (because your interpreter is likely re-exporting the Lua API already), and the second approach will link in the Lua API twice.