Hi Steve!
On 1/20/11 5:02 AM, Steve Litt wrote:
I'm glad you responded. In my research I've found no
examples of how to start in Lua and load C modules
...
trivial "hello world" application of calling C from Lua for Lua
5.1?
Here is a static Lua-C Hello World Sample / Tutorial.
This links a C extension statically into Lua. It's the other option
you have, beside dynamic linking, that ties your extension in as
closely as the Lua standard libs that need no require() call to be
available, and thus no pathes. The modification of linit.c does the
loading.
At the end there's how you integrate it into the official Lua
Makefile. That's the one in the src folder, not the one in ../src.
Best,
Henning
File ../src/hello.lua:
----------------
hellolib.hello()
File src/hello.c:
-----------------
#include <stdio.h>
#include "lauxlib.h"
static int hello(lua_State *L) {
printf("Hello World!\n");
return 1;
}
static const struct luaL_Reg hellolib [] = {
{ "hello", hello }, /* registering the function */
{ NULL, NULL }
};
LUALIB_API int luaopen_hellolib (lua_State *L) {
luaL_register(L, "hellolib", hellolib); /* registering the
module */
return 1;
}
File src/hello.h:
-----------------
#include "lua.h"
#define HELLO_LIBNAME "hellolib"
LUALIB_API int (luaopen_hellolib) (lua_State *L);
Extend file src/linit.c:
------------------------
...
#include "lauxlib.h"
#include "hello.h" /* add */
...
static const luaL_Reg lualibs[] = {
...
{"hellolib", luaopen_hellolib}, /* add */
{NULL, NULL}
...
Extend src/Makefile:
--------------------
...
LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o
loslib.o ltablib.o \
lstrlib.o loadlib.o linit.o hello.o # add
...
hello.o: hello.c # add
# (end of Makefile)
Build & Run
-----------
Build Lua from ../src/ folder, e.g. sudo make
Run src/lua hello.lua
|