lua-users home
lua-l archive

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


> I mentioned this topic is just for fun. But now, I think it seems a
> difficult issue to Lua, isn't it?
>
> In tutorial and book, the official extension method is write dynamic library
> with C/C++,
> and seldom mentioned how could we link some useful function like xml/socket
> internally.

I think it is because include a library statically inside lua is much
easier than writing a dynamic library.

I always include all libraries with my lua.

Statically include libraries inside your application does not require
any modification of your lua source, you can do all extra
initializatioin at your application's startup. You can do that after
the following line:

    lua_State *L;
    L = luaL_newstate();
    luaL_openlibs(L); /* here you init the standard lua libs */
    luaopen_foo(L); /* here you init your add-on lib */
    luaopen_bar(L); /* here you init your add-on lib */
    luaopen_baz(L); /* here you init your add-on lib */


Well, if you have a C/lua hybrid module it is a bit tough, you'll need
to convert your lua source into C data and pcall it inside your
initialization.

You can use the Linux command "xxd -i" to generate a C header file for
your lua script, include this header and you'll got a string buffer
which you can then use lua_pcall() to load the buffer.

If you are not embedding lua into your application, then the lua.c is
the host, you can just find initialization code in your lua.c. It is
better not to touch the files other than lua.c