Building Thatcher Ulrich's luaSDL for Lua 5.1 on Windows with MinGW

tung's picture

luaSDL is a library that can be loaded by Lua to make use of SDL. To get a fresh new luaSDL.dll that Lua 5.1 can load, read on!

Ingredients

Read on to get a fresh new luaSDL.dll for you to load and use!

1. Install MinGW and MSYS

Do that. Also, if you're building tolua++, add the drive it'll be on to MSYS' /etc/fstab.

2. Extract SDL Development Libraries

Do that too. You can extract them wherever you like, but keep in mind you'll be typing/copy-pasting the path a bunch, so you might want to keep it short.

3. Extract Lua 5.1.4 Windows Development Libraries

Just like SDL.

4. Obtain tolua++ for Windows

tolua++ is a utility that can take slightly-changed C or C++ headers and generate C bindings that can then be compiled into libraries that Lua can load. Thatcher Ulrich's luaSDL relies on it.

If you found a recent version of tolua++ compiled for Lua 5.1 and Windows, use it. You'll need the tolua++.exe program itself, plus the header (tolua++.h), the DLL and the linking library (ends with .a). If not, here's how I compiled it:

http://lua-users.org/wiki/CompilingToluappWithoutScons

The above link is partly correct, but the commands to make the DLL and link library no longer work. To make the DLL and linking library, do this in the MSYS shell prompt:

... /path/to/tolua++/src/lib
$ gcc -shared -o tolua++.dll -Wl,--out-implib,libtolua++.a *.c \
  <path to Lua>/lua51.dll -I../../include -I<path to Lua>/include

You'll then have tolua++.dll (runtime library) and libtolua++.a (linking library).

Building the tolua++.exe utility program itself is as described on that wiki page.

5. Make SDL.c from SDL.pkg for luaSDL using tolua++

It looks a bit like this:

... /path/to/luaSDL
$ <path to tolua++>/src/bin/tolua++.exe -o SDL.c SDL.pkg

This will create SDL.c from SDL.pkg in luaSDL.

6. Export luaopen_SDL in SDL.c

Before you compile this, you'll need to export the luaopen_SDL function so that it can be found in the final luaSDL DLL. Open SDL.c in your favourite text editor and change the bottom to look like this:

#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 501
#ifdef _WIN32
 TOLUA_API __declspec(dllexport) int luaopen_SDL (lua_State* tolua_S)
#else
 TOLUA_API int luaopen_SDL (lua_State* tolua_S)
#endif
{
 return tolua_SDL_open(tolua_S);
};
#endif

The __declspec(dllexport) part is what makes the luaopen_SDL function visible from outside the final DLL.

7. Compile luaSDL.dll

Make sure you get the arguments in the right order. In particular, SDL.o should appear before the SDL options on the command line.

... /path/to/luaSDL
$ gcc -shared -o luaSDL.dll \
  -L<path to Lua libs> -llua51 -llua5.1 \
  SDL.o \
  -L<path to SDL libs> -lSDL \
  -L <path to tolua++>/src/lib -ltolua++

This will create luaSDL.dll.

I'm not sure if both lua51 and lua5.1 are needed, or even how they're different in any way other than name. If anybody knows, I'd be happy to hear from you.

8. Using luaSDL

Put tolua.dll created from earlier in the current directory. Now luaSDL.dll can be loaded like any other binary module in Lua 5.1 using package.loadlib.

assert(package.loadlib("./luaSDL.dll", "luaopen_SDL"))()

Summary

Well, that's how you compile luaSDL for Lua 5.1 and Windows using MinGW.

I am aware of another, somewhat broader-scoped Lua-and-SDL initiative called "LuaSDL" (note the capital "L"). It might suit your needs better, but when I read the source it apparently does not do OpenGL. Who knows, maybe it's easier to add than I think?