(原+译)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 ...
随机推荐
- 洛谷 P3397 地毯
P3397 地毯 题目背景 此题约为NOIP提高组Day2T1难度. 题目描述 在n*n的格子上有m个地毯. 给出这些地毯的信息,问每个点被多少个地毯覆盖. 输入输出格式 输入格式: 第一行,两个正整 ...
- PAT 64.Complete Binary Search Tree
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1064 思路分析: 1)先对数组排好序. 2)采用中序遍历的方式,将排好序的元素逐个插入在完全 ...
- 使用yii2实现读写分离(MySQL主从数据库)
读写分离(Read/Write Splitting). 1.原理:让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELEC ...
- ecstore后台规格超过一定数量保存丢失
问题描述: 后台规格超过一定数量保存丢失,规格新增不了,删除出问题等不正常情况. 解决方法: 经过波波的不懈努力和日夜冥想终于破了.分析其原因就是因为多个规格组合生成的表单域太多,与php.ini配置 ...
- hdu 1596 find the safest road
http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...
- BZOJ 1877 晨跑
http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...
- sizeof与类,继承,virtual的种种
对虚继承层次的对象的内存布局,在不同编译器实现有所区别. 首先,说说GCC的编译器. 它实现比较简单,不管是否虚继承,GCC都是将虚表指针在整个继承关系中共享的,不共享的是指向虚基类的指针. clas ...
- Android 有缓存功能的请求封装接口
/* * @Company 浙 江 鸿 程 计 算 机 系 统 有 限 公 司 * @URL http://www.zjhcsoft.com * @Address 杭州滨江区伟业路1号 * @Emai ...
- 时间类处理<1>
2016/05/31 14:47:21 [emerg] 14629#0: location "/nginx_status" is outside location "/p ...
- 多目录下多文件 makefile编写
前面已经分享了单目录项下多文件的makefile的编写,现在来看看多目录下多文件makefile的编写: 在做项目时,一般文件都会分几个目录来存放:基本的是 include/ bin/ src/ ...