https://github.com/norman/hello-lua
hello_lua2
libnativefunc.c
#include <lua.h>
#include <lauxlib.h>
static int l_mult50(lua_State* L)
{
double number = luaL_checknumber(L, 1);
lua_pushnumber(L, number*50);
return 1;
}
int luaopen_libnativefunc(lua_State* L)
{
static const struct luaL_Reg nativeFuncLib [] =
{
{"mult50", l_mult50}, //Your function name, and the function reference after
{NULL, NULL}
};
lua_register(L, "nativelib", nativeFuncLib);
return 1;
}
lua2.lua
nativelib = require "libnativefunc"
print(nativelib.l_mult50(60))
Makefile
LUA_DIR=~/lua_test/lua-5.4.4/src
LUA_LIBDIR=~/lua_test/lua-5.4.4/src
LIBFLAG= -shared -fPIC -Wall
hello.so: libnativefunc.c
$(CC) -o libnativefunc.so $(LIBFLAG) $(CFLAGS) libnativefunc.c -I$(LUA_LIBDIR) -L$(LUA_LIBDIR)
clean:
$(RM) libnativefunc.so
test: luapi.so
shake test.lua
rock:
luarocks make luapi-scm-1.rockspec
运行
~/lua_test/lua-5.4.4/src/lua lua1.lua
标签:libnativefunc,mult50,lua,test,LUA,hello
From: https://www.cnblogs.com/hyaline-doc/p/18020910