KEEP K.I.S.S.

tk's blog

给 lua 写 C 模块示例

tk posted @ May 21, 2012 04:02:42 PM in Lua , 6584 阅读

主要内容出在《programming in lua》第二版 26章

本例是在 VS2008下,配合 lua on windows 包内的 lua 库使用。

1. 配置好 VS 的lua 环境,主要是引用的头文件目录,库目录,然后将 dll 加入 path 环境。

2. 新建一个 Win32 DLL 空项目(例如 "l_lib",这也是本例所提供的模块名),配置项目 链接器》输入》附加依赖库:lua5.1.lib

3. 添加一个 C 源文件:

#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static int l_sin(lua_State *L) 
{
    double d = luaL_checknumber(L, 1);
    lua_pushnumber(L, sin(d));
    return 1;
}

static const struct luaL_Reg l_lib[] = {
    {"l_sin", l_sin},
    {NULL, NULL}
};

int luaopen_l_lib(lua_State *L)
{
    luaL_register(L, "l_lib", l_lib);
    return 1;
} 

4. 给项目添加一个模块定义文件(.def),用于导出dll 符号:

    LIBRARY "l_lib"
    EXPORTS
        luaopen_l_lib
 
5. 编译生成: l_lib.dll
​6. 编写一个测试的 lua 文件,与生成的 dll 在同一目录:
require 'l_lib'
print(l_lib.l_sin(1)) 
 
运行如果没有报错说明一切OK
 
------------------------------------- 分割线 ----------------------------------
如果只是给个步骤不给解释是不好玩的。
所有注册到 Lua 中的函数都具有相同的原型,该原型就是定义在 lua.h 中的 lua_CFunction:
typedef int (*lua_CFunction)(lua_State *L);
从 C 语言的观点来看,这个 C 函数仅有一个参数,即 Lua 的状态。它返回一个整数,表示其压入栈中的返回值数量。因此,这个函数无须在压入结果前清空栈。在它返回后,Lua 会自动删除栈中结果之下的内容。
上面C文件里的 l_sin 函数就是我们要提供的模块内部函数,其原型与 lua_CFunction 一致。
 
lua_open_l_lib 是模块被 require 时调用的函数,是一个模块的主函数,它的原型也是与 lua_CFunction 一致,它的职责是注册模块中所有的 C 函数,并且还应初始化模块中所有需要初始化的东西。这个函数的名字是特定的,与模块名称有关。
 
luaL_Reg 结构数组是包含模块中所有函数以及名称的容器,luaL_Reg 结构有两个字段,一个函数名字符串(提供给调用模块的用户)以及一个函数指针。该数组中最后一个元素总是 {NULL, NULL} 以此标识结尾。该结构体是在辅助库内定义的。
 
模块主函数中调用了辅助库的 luaL_register 用于注册模块,它根据给定的名称("l_lib")创建一个table,并用数组 l_lib 中的信息填充这个table。在luaL_register 返回时,它会把这个table留在栈中。最后主函数返回1,表示将这个table返回给Lua。
 
通常一个模块只有一个公共函数,就是模块主函数,其他函数都应该是私有的,在 C 中就是 static。

 

  • 无匹配
  • 无匹配
KVS 1st Class Split 说:
Nov 08, 2022 04:12:57 PM

Every year, the Kendriya Vidyalaya Sangathan (KVS) publishes its Class 1 syllabus online, along with its syllabus and exam schedule. Download the Students Syllabus for the Critical Monthly Exams and the Final Exam. KVS 1st Class Split Up Syllabus KVS 1st Class Pass, Simple Way to Pass Students Purchase first-class textbooks for your institution and download the course syllabus in PDF format. Download the first-class curriculum split syllabus for 2023 from Kendriya Vidyalaya in PDF format. Exam Pattern and Syllabus for KVS First Class 2023 may be seen here.

Fantasea Show Phuket 说:
Jan 09, 2023 04:22:04 AM

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site. Fantasea Show Phuket

buy testosterone cyp 说:
Mar 10, 2023 08:58:20 AM

I found Hubwit as a transparent s ite, a social hub which is a conglomerate of Buyers and Sellers who are ready to offer online digital consultancy at decent cost. buy testosterone cypionate online

tech 说:
Dec 06, 2023 01:48:51 AM

The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. เซียนแทงบอลUFABET


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter