Skip to content

Instantly share code, notes, and snippets.

@annidy
Last active January 4, 2016 05:39
Show Gist options
  • Save annidy/8576440 to your computer and use it in GitHub Desktop.
Save annidy/8576440 to your computer and use it in GitHub Desktop.
lua扩展模块示例
all:
gcc -shared -o testc.so testc.c -llua
win:
cl /MD /O2 -c -Ftestc.obj -I"%LUA_DEV%\include" testc.c /D __WINNT__
link -dll -def:testc.def -out:testc.dll "%LUA_DEV%\lib\lua5.1.lib" testc.obj
t = require "testc"
t.test({1,2,3})
#include <lua.h>
#include <lauxlib.h>
static int test(lua_State *L)
{
if (lua_istable(L, 1)) {
lua_pushnil(L);
while(lua_next(L, 1)) {
printf("%d", lua_tointeger(L, -1));
lua_pop(L, 1);
}
}
return 0;
}
static const luaL_Reg testclib[] = {
{"test", test},
{NULL, NULL},
};
int luaopen_testc(lua_State *L)
{
luaL_register(L, "testc", testclib);
return 1;
}
LIBRARY testc
EXPORTS
luaopen_testc @1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment