Compiling Lua from Source: A Step-by-Step Guide
As I prepared to delve into the world of Lua coroutines, I stumbled upon a piece of code that would only run under Lua version 5.1.4, resulting in the infamous “lua: attempt to yield across metamethod/C-call boundary” error. This issue is said to occur when the main thread calls the yield function. In an effort to resolve this issue, I decided to explore the possibility of using a version above 5.1.4, specifically version 5.2. However, I was hesitant to use the Visual Studio (VS) compiled lua.exe, leading me to search for alternative methods of generating static libraries and dynamic libraries using the command line.
Generating Static Libraries and Dynamic Libraries
After conducting extensive research, I uncovered the necessary commands to generate static libraries and dynamic libraries using the command line. These commands are as follows:
Generating Static Libraries
To generate a static library for Lua 5.1.4, navigate to the src
directory and execute the following command:
del * .obj liblua.lib
cl -c -nologo -O2 -Ob1 -Oi -Gs -MT lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c lstrlib.c loadlib.c linit.c
link -lib -out:liblua.lib -verbose: lib * .obj
Generating Dynamic Libraries
To generate a dynamic library for Lua 5.1.4, navigate to the src
directory and execute the following command:
del * .obj liblua.dll
cl -c -nologo -O2 -Ob1 -Oi -Gs -MT -DLUA_BUILD_AS_DLL lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c lstrlib.c loadlib.c linit.c
link -dll -out:liblua.dll -verbose: lib * .obj
Generating lua.exe and luac.exe
To generate the lua.exe and luac.exe files, navigate to the src
directory and execute the following commands:
del * .obj lua.exe
cl -c -nologo -O2 -Ob1 -Oi -Gs -MT lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c lstrlib.c loadlib.c linit.c
link -link -out:lua.exe -verbose: lib * .obj
del * .obj luac.exe
cl -c -nologo -O2 -Ob1 -Oi -Gs -MT lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c
link -link -out:luac.exe -verbose: lib * .obj
Compiling Lua 5.2.3
When compiling Lua 5.2.3, the changes are quite significant. According to the documentation in the doc
directory, the following commands can be used to generate the lua.exe file:
del * .obj lua.exe
cl -c -nologo -O2 -Ob1 -Oi -Gs -MT lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c
link -link -out:lua.exe -verbose: lib * .obj
Building Lua on Other Systems
If you are not using the standard Unix tools, the instructions for building Lua will depend on the compiler you are using. You will need to create projects (or whatever your compiler uses) for building the library, interpreter, and compiler, as follows:
Library
lapi.c
lcode.c
lctype.c
ldebug.c
ldo.c
ldump.c
lfunc.c
lgc.c
llex.c
lmem.c
lobject.c
lop_codes.c
lparser.c
lstate.c
lstring.c
ltable.c
ltm.c
lundump.c
lvm.c
lzio.c
lauxlib.c
lbaselib.c
lbitlib.c
lcorolib.c
ldblib.c
liolib.c
lmathlib.c
loslib.c
lstrlib.c
ltablib.c
loadlib.c
linit.c
Interpreter
library
lua.c
Compiler
library
luac.c
Common Errors
When compiling Lua, you may encounter the following errors:
linit.obj: error LNK2001: unresolved external symbol _luaopen_coroutine
linit.obj: unresolved external symbol _luaopen_bit32
To resolve these issues, you can try the following:
- Run the script
vsvars32.bat
to set the environment variables. - Refer to the documentation for the specific compiler you are using.
- Check the
README.html
file in thedoc
directory for further instructions.
By following these steps, you should be able to successfully compile Lua from source and generate the necessary static libraries and dynamic libraries.