lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


--- James Hearn <james.hearn@byu.edu> wrote:
> Right this minute I'm working on compiling a completely native
> dynamically loaded version of Lua and the loadlib test program using the
> cygwin toolchain. I'm making progress. Wish me luck!

Just for contrast, this is all easy to do with Borland C++ 5.5 (free).
>From the root of the Lua distribution:

(1) Build lua.dll (containing Lua API and libraries):

  bcc32 -elua.dll -Iinclude -DLUA_API=__declspec(dllexport) \
  -DLUALIB_API=__declspec(dllexport) -tWD src/*.c src/lib/*.c

(2) Create an import library fo lua.dll:

  implib lua.lib lua.dll

(3) Create a version of lua.exe that gets Lua from lua.dll:

  bcc32 -elua.exe -Iinclude -DLUA_API=__declspec(dllimport) \
  -DLUALIB_API=__declspec(dllimport) lua.lib src/lua/*.c

---------------------------------------------------------------------------

To try out the loadlib stuff: extract the 'loadlib' files to a directory
named 'loadlib' (in the root of the Lua distro), then rebuild lua.dll with
'loadlib\etc\windows.c' added:

  bcc32 -elua.dll -Iinclude -DLUA_API=__declspec(dllexport) \
  -DLUALIB_API=__declspec(dllexport) -tWD src/*.c src/lib/*.c \
  loadlib/etc/windows.c

Rebuild lua.exe - add 'lua_loadlibopen(l);' to openstdlibs in lua.c, then
repeate steps (2) and (3) above.

Build mylib.dll. mylib.c does not export mylib_start; you must add
__declspec(dllexport) before that name before you build.  Then, to build:

  bcc32 -Iinclude -DLUA_API=__declspec(dllimport) -tWD lua.lib \
  loadlib\mylib.c

That's it. You can run 'lua loadlib\test.lua'. Output: 'Hello from mylib'.

On complication: Borland C++ mangles the names __cdecl functions by
prepending an underscore. This means mylib.dll will actually export
_mylib_start instead of mylib_start.  You can either change test.lua to run
'_mylib_start', or create a module definition file for mylib.dll that maps
the exported symbol to the appropriate name.

mylib.def: EXPORTS mylib_start = _mylib_start

Cheers, Eric

P.S. In case that's a confusing jumble. Here's the short version of
building the Windows loadlib example with Borland C++ 5.5 (with the Lua
4.1 work 4):

Extract the 'loadlib' tarball into '(lua distro root)\loadlib', then..

Code changes: 

(1) Add the following line to include\lualib.h

  LUALIB_API int lua_loadlibopen (lua_State *L);

(2) Add the following line to src\lua\lua.c : 'openstdlibs'

  lua_loadlibopen(l);

(3) Change the 'mylib_start' line in loadlib\mylib.c to:

  __declspec(dllexport) int mylib_start(lua_State *L)

(4) Change the 'loadlib' line in loadlib\test.lua to:

  assert(loadlib(D.."\\mylib.dll","_mylib_start"))

(5) Build everything:

  bcc32 -elua.dll -Iinclude -DLUA_API=__declspec(dllexport) \
  -DLUALIB_API=__declspec(dllexport) -tWD src/*.c src/lib/*.c
  loadlib/etc/windows.c
  implib lua.lib lua.dll
  bcc32 -elua.exe -Iinclude -DLUA_API=__declspec(dllimport) \
  -DLUALIB_API=__declspec(dllimport) lua.lib src/lua/*.c
  bcc32 -Iinclude -DLUA_API=__declspec(dllimport) -tWD lua.lib \
  loadlib\mylib.c


__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/