(原+译)LUA调用C函数
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/5804924.html
原始网址:
http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm
1. 新建hellofunc.c,输入:
/* hellofunc.c (C) 2011 by Steve Litt
* gcc -Wall -shared -fPIC -o Cfunc.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c
* Note the word " Cfunc " matches the string after the underscore in
* function luaopen_ Cfunc(). This is a must.
* The -shared arg lets it compile to .so format.
* The -fPIC is for certain situations and harmless in others.
* On your computer, the -I and -l args will probably be different.
*/ #include <lua5./lua.h> /* Always include this */
#include <lua5./lauxlib.h> /* Always include this */
#include <lua5./lualib.h> /* Always include this */ static int isquare(lua_State *L) /* Internal name of func */
{
float rtrn = lua_tonumber(L, -); /* Get the single number arg */
printf("Top of square(), nbr=%f\n",rtrn);
lua_pushnumber(L,rtrn*rtrn); /* Push the return */
return ; /* One return value */
}
static int icube(lua_State *L){ /* Internal name of func */
float rtrn = lua_tonumber(L, -); /* Get the single number arg */
printf("Top of cube(), number=%f\n",rtrn);
lua_pushnumber(L,rtrn*rtrn*rtrn); /* Push the return */
return ; /* One return value */
} /* Register this file's functions with the
* luaopen_libraryname() function, where libraryname
* is the name of the compiled .so output. In other words
* it's the filename (but not extension) after the -o
* in the cc command.
*
* So for instance, if your cc command has -o power.so then
* this function would be called luaopen_power().
*
* This function should contain lua_register() commands for
* each function you want available from Lua.
*
*/
int luaopen_Cfunc(lua_State *L){
lua_register(
L, /* Lua state variable */
"testsquare", /* func name as known in Lua */
isquare /* func name in this file */
);
lua_register(L,"testcube",icube);
return ;
}
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())
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后,也能显示正确的结果,如下:
(原+译)LUA调用C函数的更多相关文章
- Step By Step(Lua调用C函数)
原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...
- [原][译][lua][luabridge]一个简单的luabridge与c++例子结合例子
参考:https://eliasdaler.wordpress.com/tag/luabridge/ https://eliasdaler.wordpress.com/2015/08/10/using ...
- lua调用c++函数返回值作用
2015/05/28 lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值.但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数. (1) ...
- cocos2dx中使用tolua++使lua调用c++函数
一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来: 首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程, ...
- lua调用c函数
参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...
- Lua中调用C函数
Lua利用一个虚拟的堆栈来给C传递值或从C获取值.每当Lua调用C函数,都会获得一个新的堆栈,该堆栈初始包含所有的调用C函数所需要的参数值(Lua传给C函数的调用实参),并且C函数执行完毕后,会把返回 ...
- [置顶] lua 进阶3--lua文件中调用C++函数
前面讲了一下,C++读取lua文件中的变量,包括一维表.二维表这些,这节讲一下如何在lua文件中去调用C++函数 C++代码如下 #include <stdio.h> extern &qu ...
- lua中调用C++函数
lua中调用C++函数 我们产品中提供了很多lua-C API给用户在lua中调用,之前一直没用深究其实现原理,只是根据已有的代码在编码.显然这不是一个好的习惯,没用达到知其所以然的目的. 一.基本原 ...
- lua调用C语言
在上一篇文章(C调用lua函数)中,讲述了如何用c语言调用lua函数,通常,A语言能调用B语言,反过来也是成立的.正如Java 与c语言之间使用JNI来互调,Lua与C也可以互调. 当lua调用c ...
随机推荐
- Qt窗口句柄
关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄
- Mysql学习(慕课学习笔记1)启动、登录及常用命令
Mysql学习 启动数据库服务 net start mysql (不能加分号!!!!) 关闭数据库服务 net stop mysql 登录数据库 mysql -uroot -p -P3306 - ...
- Creating a settings table that can handle almost any type of value
Update: Updated article here. Today I wanted to be able to have a table store any type of value as a ...
- 模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- Strange Grid
def main(): r,c = map(int, raw_input().split(' ')) if r % 2 != 0: base = 5*(r-1) else: base = 5*(r-2 ...
- 这样就算会了PHP么?-11
PHP中关于类的基本内容练习: <?php class SportObject{ public $name; public $height; public $avirdupois; public ...
- 继承Object和ContextBoundObject处理效率上的差距
继承Object和ContextBoundObject处理效率上的差距 ContextBoundObject一个很熟悉的对象,它提供对象有处理上下文的能力:通过它能够方便地实现AOP处理机制.它带来好 ...
- mysql 中文配置(转)
Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...
- VIm变成sublime (转)
sublime在ubuntu下始终支持不是很好, 特别是对中文输入的支持,还有一些插件在ubuntu下也不能用. 在ubuntu下还是用vim吧. 我们一起把vim变成sublime. 只需要三步 ...
- 反转int型数字
如 321 反转 123 120 反转21 注意处理最后的零,以及负数情况 ,最后就是溢出情况了 /** * @param {number} x * @return {number} */ var r ...