Skip to content

Compiling C Lua Libraries

Cody Tilkins edited this page Feb 26, 2021 · 4 revisions

Table of Contents

Directory Listing

Following this thread, your directory tree will look like the following.

MSVS

./res/testing.lua
./lang/*
./jit/*
./dll/
./lua_example.c
./ltest.luajit.lua
./ltest.lua542.lua
./luaw.exe
./lua.ico
./liblcluajit.dll
./liblclua-5.4.2.dll
./libluajit.dll
./libluajit.lib
./libluajit.exp
./liblua-5.4.2.dll
./liblua-5.4.2.lib
./liblua-5.4.2.exp
./libs/luajit/example.dll
./libs/lua542/example.dll

MingW64

./res/testing.lua
./lang/*
./jit/*
./dll/
./lua_example.c
./ltest.luajit.lua
./ltest.lua542.lua
./luaw.exe
./lua.ico
./liblcluajit.dll
./liblclua-5.4.2.dll
./libluajit.dll
./liblua-5.4.2.dll
./libs/luajit/example.dll
./libs/lua542/example.dll

Linux

./res/testing.lua
./lang/*
./jit/*
./dll/
./lua_example.c
./ltest.luajit.lua
./ltest.lua542.lua
./luaw
./lua.ico
./liblcluajit.so
./liblclua-5.4.2.so
./libluajit.so
./liblua-5.4.2.so
./libs/luajit/example.so
./libs/lua542/example.so

C Lua Library

lua_example.c

#include <stdio.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"


#if defined(_MSC_VER)
#	define LUA_EXPORT __declspec(dllexport)
#else
#	define LUA_EXPORT __attribute__((dllexport))
#endif


int luaexport_example(lua_State* L) {
	puts("Hello World!");
	return 0;
}

LUA_EXPORT int luaopen_example(lua_State* L) {
	lua_pushcclosure(L, luaexport_example, 0);
	lua_setglobal(L, "example");
	return 0;
}

Lua Test File

ltest.luajit.lua

require("libs.luajit.example")

example()

ltest.luajit.lua

require("libs.lua542.example")

example()

Lua Run Script

Windows

luaw.exe ltest.luajit.lua
luaw.exe -w luajit -lltest.luajit.lua
luaw.exe -w lua-5.4.2 -lltest.luajit.lua

Linux

# Only need if self contained and following this thread
LD_LIBRARY_PATH=".:$LD_LIBRARY_PATH"

./luaw ltest.luajit.lua
./luaw -w luajit -lltest.luajit.lua
./luaw -w lua-5.4.2 -lltest.luajit.lua

MSVS

LuaConsole outputs a .lib .exp and .dll for each Lua version you package - both when creating the driver and packages. You will need to take the generated .lib and link against it. If you packaged luajit, you need the libluajit.lib for example. After building, these can be found in the bin directory.

LuaConsole Build Script

REM vcvars64.bat

build.msvs.bat driver luajit
build.msvs.bat package lua-5.4.2

Build Script

REM vcvars64.bat

mkdir libs
mkdir libs\luajit
mkdir libs\lua542

cl.exe /O2 /D_USRDLL /D_WINDLL lua_example.c libluajit.dll /link /DLL /OUT:example.dll
move example.dll libs\luajit
move example.lib libs\luajit
move example.exp libs\luajit

cl.exe /O2 /D_USRDLL /D_WINDLL lua_example.c liblua-5.4.2.dll /link /DLL /OUT:example.dll
move example.dll libs\lua542
move example.lib libs\lua542
move example.exp libs\lua542

MinGW64

LuaConsole outputs a .dll for each Lua version you package - both when creating the driver and packages. You will need to take the generated .dll and link against it. If you packaged luajit, you need the libluajit.dll for example. After building, these can be found in the bin directory.

LuaConsole Build Script

build.mingw.bat driver luajit
build.mingw.bat package lua-5.4.2

Build Script

mkdir libs
mkdir libs\luajit
mkdir libs\lua542

gcc -Wall -O2 -g0 -shared -o example.dll lua_example.c libluajit.dll
move example.dll libs\luajit

gcc -Wall -O2 -g0 -shared -o example.dll lua_example.c liblua-5.4.2.dll
move example.dll libs\lua542

Linux

LuaConsole outputs a .so for each Lua version you package - both when creating the driver and packages. You will need to take the generated .so and link against it. If you packaged luajit, you need the libluajit.so for example. After building, these can be found in the bin directory.

LuaConsole Build Script

./build.linux.sh driver luajit
./build.linux.sh package lua-5.4.2

Build Script

mkdir -p libs/luajit
mkdir -p libs/lua542

gcc -Wall -O2 -g0 -fPIC -shared -Wl,-E -o example.so lua_example.c libluajit.dll
mv example.so libs/luajit

gcc -Wall -O2 -g0 -fPIC -shared -Wl,-E -o example.so lua_example.c liblua-5.4.2.dll
mv example.so libs/lua542