c 调用 lua 向lua函数 传递table

参考 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;  
}  
原文地址:https://www.cnblogs.com/taek/p/8380154.html