Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1411690
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: LINUX

2015-04-16 21:59:31

1、C调用Lua

点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include <process.h>
  3. #include <stdio.h>
  4. extern "C" {
  5. #include "lua.h"
  6. #include "lualib.h"
  7. #include "lauxlib.h"
  8. }
  9. #pragma comment(lib, "lua5.1.lib")
  10. lua_State* L;

  11. int main ( int argc, char *argv[] )
  12. {
  13.     /* initialize Lua */
  14.     L = lua_open();
  15.     /* load Lua base libraries */
  16.     luaL_openlibs(L);
  17.     luaL_dofile(L, "my.lua");//载入代码文件并执行
  18.     //假设里面的函数是这样的:function LuaFun(a,b) return a+b,a-b end

  19.     lua_getglobal(L, "LuaFun");
  20.     lua_pushnumber(L, 10);//压入参数1
  21.     lua_pushnumber(L, 20);//压入参数2
  22.     lua_call(L, 2, 2);//调用函数,2个参数,2个返回值
  23.     int a = luaL_checkinteger(L, 1);//第一个返回值
  24.     int b = luaL_checkinteger(L, 2);//第一个返回值
  25.     lua_pop(L, 2);//清理堆栈
  26.     printf("a:%d,b:%d\n",a,b);
  27.     /* cleanup Lua */
  28.     lua_close(L);
  29.     /* pause */
  30.     printf( "Press enter to exit..." );
  31.  
  32.     system("pause");
  33.     return 0;

  34. }

点击(此处)折叠或打开

  1. function
  2. LuaFun(a,b)
  3. return a+b,a-b
  4. end

2、Lua调用C

点击(此处)折叠或打开

  1. function sum(a,b)
  2.     return a+b
  3. end

  4. function mystrcat(a,b)
  5.     return a..b
  6. end

  7. function mysum(a,b)
  8.     return csum(a,b)
  9. end


点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>


  6. extern "C"{
  7. #include <lua.h>
  8. #include <lualib.h>
  9. #include <lauxlib.h>
  10. };


  11. #pragma comment(lib, "lua5.1.lib")




  12. int csum(lua_State *l)
  13. {
  14.     int a=lua_tointeger(l,1);
  15.     int b=lua_tointeger(l,2);
  16.     lua_pushinteger(l,a+b);
  17.     return 1;
  18. }


  19. int _tmain(int argc, _TCHAR* argv[])
  20. {

  21.     lua_State * l = luaL_newstate() ; //创建lua运行环境
  22.     if ( l == NULL ) return 0;
  23.     int ret = 0 ;
  24.     ret = luaL_loadfile(l,"test.lua") ; //加载lua脚本文件
  25.     if ( ret != 0 ) return 0;
  26.     ret = lua_pcall(l,0,0,0) ;
  27.     if ( ret != 0 ) return 0;
  28.     lua_getglobal(l,"width"); //获取lua中定义的变量
  29.     lua_getglobal(l,"height");
  30.     printf("height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
  31.     lua_pop(l,1) ; //恢复lua的栈
  32.     int a = 11 ;
  33.     int b = 12 ;
  34.     lua_getglobal(l,"sum"); //调用lua中的函数sum
  35.     lua_pushinteger(l,a) ;
  36.     lua_pushinteger(l,b) ;
  37.     ret = lua_pcall(l,2,1,0) ;//两个参数,一个返回值
  38.     if ( ret != 0 ) return 0;
  39.     printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
  40.     lua_pop(l,1) ; //恢复lua的栈
  41.     const char str1[] = "hello" ;
  42.     const char str2[] = "world" ;
  43.     lua_getglobal(l,"mystrcat"); //调用lua中的函数mystrcat
  44.     lua_pushstring(l,str1) ;
  45.     lua_pushstring(l,str2) ;
  46.     ret = lua_pcall(l,2,1,0) ;//两个参数,一个返回值
  47.     if ( ret != 0 ) return 0;
  48.     printf("mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
  49.     lua_pop(l,1) ; //恢复lua的栈
  50.     lua_pushcfunction(l,csum) ; //注册在lua中使用的c函数
  51.     lua_setglobal(l,"csum") ; //绑定到lua中的名字csum
  52.     lua_getglobal(l,"mysum"); //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法
  53.     lua_pushinteger(l,a) ;
  54.     lua_pushinteger(l,b) ;
  55.     ret = lua_pcall(l,2,1,0) ;
  56.     if ( ret != 0 ) return 0;
  57.     printf("mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
  58.     lua_pop(l,1) ;
  59.     lua_close(l) ; //释放lua运行环境

  60.     return 0;
  61. }



阅读(645) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~