[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Linking LuaSocket statically
- From: Diego Nehab <diego@...>
- Date: Wed, 28 Jul 2010 23:53:44 -0300
Hi,
First, let's refresh how to statically link a module in Lua. Say the
module would be invoked as
require"foo"
If it is a C module, then there is a C entrypoint luaopen_foo().
Link the object files with your application and, during your
initialization code, produce a Lua function from this entrypoint and
assign it to preload["foo"] (preload is a table that lives in the
registry). This will make sure require() doesn't look for the module
on disk. It checks that table first.
If your module is a Lua module, you first need to compile the Lua
module using luac, then convert it to something that can be included
in a C file with bin2c. You can then create a C entrypoint that is
equivalent to running the Lua module. Compile this C file and link to
your application, then put a Lua function corresponding to that
entrypoint in preload["foo"].
LuaSocket has both C and Lua modules. Some modules, such as mime and
socket, are hybrid. When you invoke
require"socket"
you are actually loading socket.lua. This Lua file invokes
require"socket.core", which loads luasocket.c. (same thing with
mime.lua, which loads mime.c).
To link statically to LuaSocket and have require"socket" work, you
have to do the following. Put a Lua function corresponding to the
entrypoint luaopen_socket_core, found in luasocket.c, in
preload["socket.core"]. You also have to put a Lua function
corresponding to socket.lua (made with luac and bin2c) in
preload["socket"].
You also have to put the entrypoint corresponding to http.lua in
preload["http"], the one corresponding to smtp.lua in preload["smtp"]
and etc.
I think this sums up the process. I have never personally done this
myself, but I have instructed several people off-the-list on how to do
it and they all managed. If anybody has doubts, please post on this
thread so it stays on record forever. :)
Regards,
Diego