• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c 调用 lua 向lua函数 传递table

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

参考 https://www.myvoipapp.com/blogs/yxh/2016/07/14/c%E5%90%91lua%E5%87%BD%E6%95%B0%E4%BC%A0%E9%80%92table%E5%8F%82%E6%95%B0/

1.lua

function showstr(str2)  
print("The string you input is " .. str2.name)  
end 

 

1.c

 gcc -o 1 1.c  -llua-5.1
#include <stdio.h>  
  
  
//lua头文件  
#ifdef __cplusplus  
    extern "C" {  
#include "lua.h"    
#include <lauxlib.h>     
#include <lualib.h>     
}    
#else  
#include <lua.h>  
#include <lualib.h>  
#include <lauxlib.h>  
#endif  
 
  /*
  lua -> c 
  https://www.cnblogs.com/coderkian/p/4057750.html

    https://www.cnblogs.com/pied/archive/2012/10/26/2741601.html
    gcc -o lua lua.c  -llua-5.1

    https://www.cnblogs.com/sevenyuan/p/4511808.html
  */
  
int main(int argc,char ** argv)  
{  
  
    lua_State * L=NULL;  
  
    /* 初始化 Lua */    
    L = lua_open();    
  
    /* 载入Lua基本库 */    
    luaL_openlibs(L);     
  
     
    /* 运行脚本 */    
   int error = luaL_dofile(L, "./1.lua");
    if(error) {
        perror("luaL_dofile error");
        return 1;
    }

    lua_getglobal(L,"showstr"); 
    lua_newtable(L); // 创建一个table
    lua_pushstring(L, "name");  //key为intVal
    lua_pushinteger(L,1234);      //值为1234
    lua_settable(L, -3);          //写入table

    lua_pcall(L,1,0,0);  
    
    /* 清除Lua */    
    lua_close(L);     
  
    return 1;  
}  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
windows下gvim/vimlua支持问题,neocomplete等插件支持发布时间:2022-07-22
下一篇:
lua中文教程【高级知识】发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap