(原+译)LUA调用C函数
函数 调用 lua
2023-09-11 14:22:24 时间
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/5804924.html
原始网址:
http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm
1. 新建hellofunc.c,输入:
1 /* hellofunc.c (C) 2011 by Steve Litt 2 * gcc -Wall -shared -fPIC -o Cfunc.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c 3 * Note the word " Cfunc " matches the string after the underscore in 4 * function luaopen_ Cfunc(). This is a must. 5 * The -shared arg lets it compile to .so format. 6 * The -fPIC is for certain situations and harmless in others. 7 * On your computer, the -I and -l args will probably be different. 8 */ 9 10 #include <lua5.1/lua.h> /* Always include this */ 11 #include <lua5.1/lauxlib.h> /* Always include this */ 12 #include <lua5.1/lualib.h> /* Always include this */ 13 14 static int isquare(lua_State *L) /* Internal name of func */ 15 { 16 float rtrn = lua_tonumber(L, -1); /* Get the single number arg */ 17 printf("Top of square(), nbr=%f\n",rtrn); 18 lua_pushnumber(L,rtrn*rtrn); /* Push the return */ 19 return 1; /* One return value */ 20 } 21 static int icube(lua_State *L){ /* Internal name of func */ 22 float rtrn = lua_tonumber(L, -1); /* Get the single number arg */ 23 printf("Top of cube(), number=%f\n",rtrn); 24 lua_pushnumber(L,rtrn*rtrn*rtrn); /* Push the return */ 25 return 1; /* One return value */ 26 } 27 28 29 /* Register this file's functions with the 30 * luaopen_libraryname() function, where libraryname 31 * is the name of the compiled .so output. In other words 32 * it's the filename (but not extension) after the -o 33 * in the cc command. 34 * 35 * So for instance, if your cc command has -o power.so then 36 * this function would be called luaopen_power(). 37 * 38 * This function should contain lua_register() commands for 39 * each function you want available from Lua. 40 * 41 */ 42 int luaopen_Cfunc(lua_State *L){ 43 lua_register( 44 L, /* Lua state variable */ 45 "testsquare", /* func name as known in Lua */ 46 isquare /* func name in this file */ 47 ); 48 lua_register(L,"testcube",icube); 49 return 0; 50 }
2. 在ubuntu的终端中生成.so:
gcc -Wall -shared -fPIC -o Cfunc.so hellofunc.c
说明:luaopen_ XXX对应于上面-o后面的XXX.so的XXX。供LUA代码中require“XXX”来调用。
3. 新建myLUA.lua,输入:
require("Cfunc") print(testsquare(1.414213598)) print(testcube(5))
4. 终端中输入:
lua myLUA.lua
5. 结果:
说明:电脑上装了lua5.1和lua5.2.默认的是lua5.2.使用lua myLUA.lua后,提示:
经过查找,发现默认的是lua5.2。然后直接使用lua5.1 myLUA.lua后,显示的就是正确的结果(如果使用lua5.2 myLUA.lua,显示的就是上面的错误)。当然,如果装了torch,torch默认的也是5.1的话,使用th myLUA.lua后,也能显示正确的结果,如下:
相关文章
- oracle存储过程函数
- 模块嵌套调用,当有嵌套调用的时候,二级嵌套文件的路径,一定是相对入口函数
- 数据库分析函数 ROW_NUMBER() rank() dense_rank() 的区别 first_value(D) , last_value(D)
- Matlab中zlim函数的使用
- Google Earth Engine(GEE)——function中函数注意事项(内部变量不可在函数外调用)
- jquery技术揭秘静态工具函数源码重构
- Python-OpenCV难道比C++_OpenCV快?以调用Canny边缘检测函数为例
- Python的灵活-允许嵌套定义函数并在内层函数的层级上直接调用内层函数
- C语言sprintf 和 sscanf函数
- 解决触发器检测到活动的独立的事务处理, 已经回退、表发生了变化, 触发器/函数不能读它
- oracle的 listagg() WITHIN GROUP ()函数使用
- qt中集成object的对象movethread后对象里面成成员函数执行所在的线程问题(不是信号槽机制的哪个线程调用就所属与哪个线程)
- Python基础的高阶函数和闭包
- tensorflow 的tf.split函数的用法
- js中的三个编码函数:escape,encodeURI,encodeURIComponent
- C++ 虚函数表解析_转载于陈皓
- nodejs进阶(2)—函数模块调用
- python 函数定义与调用时,不定长参数的传入
- oracle函数 exp(y)
- 函数式编程—函数的关系—is-a、has-a、use-a
- Linux socket编程中调用 inet_ntoa 函数产生的段错误 “Segmentation fault (core dumped)” 的原因及解决办法
- JavaScript函数及方法的定义与调用
- CV2:调用不了CV2里面的函数
- shell流程控制(流程不可为空、if else流程、for循环、while循环/无限循环、until循环、分支结构case...esac、跳出循环)、shell 函数(定义需在开头、如何调用、有无return返回值差别、函数参数$1 / ${10}、$? 获取返回值及其限制)、shell 输入输出重定向、Here Document重定向方式、/dev/null文件作用、shell文件包含及使用
- ES6里关于函数的拓展(二)
- c#扩展函数
- (原)lua使用ffi调用c程序的函数
- C++调用空指针对象的成员函数成功的理解