转载请注明出处:

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函数的更多相关文章

  1. Step By Step(Lua调用C函数)

    原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...

  2. [原][译][lua][luabridge]一个简单的luabridge与c++例子结合例子

    参考:https://eliasdaler.wordpress.com/tag/luabridge/ https://eliasdaler.wordpress.com/2015/08/10/using ...

  3. lua调用c++函数返回值作用

    2015/05/28 lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值.但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数. (1) ...

  4. cocos2dx中使用tolua++使lua调用c++函数

    一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来: 首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程, ...

  5. lua调用c函数

    参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...

  6. Lua中调用C函数

    Lua利用一个虚拟的堆栈来给C传递值或从C获取值.每当Lua调用C函数,都会获得一个新的堆栈,该堆栈初始包含所有的调用C函数所需要的参数值(Lua传给C函数的调用实参),并且C函数执行完毕后,会把返回 ...

  7. [置顶] lua 进阶3--lua文件中调用C++函数

    前面讲了一下,C++读取lua文件中的变量,包括一维表.二维表这些,这节讲一下如何在lua文件中去调用C++函数 C++代码如下 #include <stdio.h> extern &qu ...

  8. lua中调用C++函数

    lua中调用C++函数 我们产品中提供了很多lua-C API给用户在lua中调用,之前一直没用深究其实现原理,只是根据已有的代码在编码.显然这不是一个好的习惯,没用达到知其所以然的目的. 一.基本原 ...

  9. lua调用C语言

    在上一篇文章(C调用lua函数)中,讲述了如何用c语言调用lua函数,通常,A语言能调用B语言,反过来也是成立的.正如Java 与c语言之间使用JNI来互调,Lua与C也可以互调.   当lua调用c ...

随机推荐

  1. 头文件 .h 与源文件 .ccp 的区别

    .h 文件一般是用来定义的,比如定义函数.类.结构体等: .cpp 文件则是对头文件的定义进行实现. include .h文件,可以调用你声明的函数.类等.当然,比较简单的类.函数,你也可以直接在头文 ...

  2. requestAnimationFrame动画方法

    一.动画方式 在HTML5/CSS3时代,实现动画的方式有许多种: 你可以用css3的animation和@keyframes: 可以用css3的transition: 还可以用原始的setTimeo ...

  3. 记事本创建servlet在tomcat中发布基本思路

    在webapps中新建文件夹H,在其中再创建WEB-INF文件夹,在创建classes文件夹和web.xml文件,web.xml需要配置一下,classes文件夹中存放Servlet经编译过的clas ...

  4. ng-repeat-start ng-repeat-end 的使用

    ng-repeat-start与ng-repeat-end时AngularJS(1.2.x)扩展的, 使用这两个指令可以灵活控制遍历形式. 例如: index.html <div class=& ...

  5. JVM virtual memory

    This has been a long-standing complaint with Java, but it's largely meaningless, and usually based o ...

  6. IPTABLES封闭和开放端口

    这个是个细心活儿,如果只开放特定端口,则在每一条规则里都要加上相应的端口. 任何一条不配DPORT,都有可能放行了其它端口. -A INPUT -p tcp -m tcp --dport 80 -m ...

  7. XJOI网上同步训练DAY3 T1

    思路:看来我真是思博了,这么简单的题目居然没想到,而且我对复杂度的判定也有点问题.. 首先我们选了一个位置i的b,那一定只对i和以后的位置造成改变,因此我们可以这样看: 我们从前往后选,发现一个位置的 ...

  8. C51 函数/程序段的定位

    在Keil C中可能需要指定某个函数或者某段程序链接后存放在程序区中的位置. 1. 如何指定某个函数在程序区中的位置. QUESTION How do I locate a C function at ...

  9. Could not find qmake configuration file win32-g++

    D:\Source>c:\Qt\Qt5.3.2_static\bin\qmake -makefile -o Makefile my.proCould not find qmake configu ...

  10. bzoj1060 [ZJOI2007]时态同步

    Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板 ...