This is mentioned in Lua programming, but it is not that simple for beginners to successfully execute Lua. The following describes how to generate a dynamic link library so file. The export function in lua5.2 is changed from lual_register to lual_newlib. Specific details need to be further explored. The module name here is hello_lib. The Lua interpreter will find the corresponding module based on the name and then execute the luaopen_xxx method. Code:
# Include <math. h> # include <lua5.2/Lua. h> # include <lua5.2/lauxlib. h> # include <lua5.2/lualib. h> static int hello_sin (lua_state * l) {double D = lual_checknumber (L, 1); lua_pushnumber (L, sin (d); return 1 ;} static const struct lual_reg hello_lib [] = {"hello_sin", hello_sin}, {null, null}; int luaopen_hello_lib (lua_state * l) {lual_newlib (L, hello_lib ); // lual_register (L, "hello_lib", hello_lib); // Lua 5.1 return 1 ;}
Call in Lua:
Local Hello = require "hello_lib" Print (hello. hello_sin (1 ))
Execution process and result:
Lua calls the custom C Module