zoukankan      html  css  js  c++  java
  • Lua 跟 C++ 的交互

    Lua 和 C++ 是这样交互的

    乱七八糟的前戏:
    1. 到官网下载 Lua 文件  可參考 ->   Lua 下载与配置
    2. 设置环境  可參考  ->   VS 配置Lua环境


    交互过程有:
    1. C++ 訪问 Lua 的变量
    2. C++ 调用 Lua 的函数
    3. Lua 訪问 C++ 的变量
    4. Lua 訪问 C++ 的函数


    No code say a j8


    C++ 调用 Lua

    #include <iostream>
    #include <string>
    
    // 引入Lua必要的头文件,Version: Lua5.1.5
    extern "C"
    {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    };
    
    using namespace std;
    
    // C++ 调用 lua 
    int main()
    {
    	//初始化全局L
    	lua_State *L = luaL_newstate();
    
    	//打开库
    	luaL_openlibs(L);
    
    	//载入lua脚本文件
    	if (luaL_loadfile(L,"LuaFile\lua.lua"))   // lua.lua 的路径,这里使用相对路径
    	{
    		printf("file load error
    ");
    	}
    
    	lua_pcall(L,0,0,0);    // 载入 Lua 文件
    
    	lua_getglobal(L, "l_str");  // get, 将L指向 lua文件里的函数 l_str 
    
    	lua_pcall(L,0,1,0);		// 运行指针L指向的函数,将结果返回到栈顶, (0,1,0) 表示 (输入个数,输出个数,其它处理)
    
    	string strVersion = luaL_checkstring(L,1);   // 从栈顶获取元素
    	cout<<strVersion<<endl;
    	
    	lua_close(L);
    
    	return 0;
    }
    
    
    /*************************************
    Lua.lua
    
    version = "Lua version: 5.1.5";
    function l_str()
    	return version;
    end;
    
    
    print("Load LuaFile Accomplish");
    
    *************************************/
    


    Lua 调用 C++

    #include <iostream>
    #include <string>
    
    // 引入Lua必要的头文件,Version: Lua5.1.5
    extern "C"
    {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    };
    
    using namespace std;
    
    
    // 将lua中的变量,通过这个函数打印出来
    int c_Print(lua_State* L)	// 返回值为压入栈中元素的个数	
    {
    	// 从參数列表中,获取元素
    	string strVersion = luaL_checkstring(L,1);
    
    	cout<<strVersion<<endl;
    
    	lua_pushstring(L, strVersion.c_str());		// 将 strVersion 压入栈。也能够不压
    
    	return 1;    // 返回压入栈中元素的个数
    
    }
    
    // Lua 调用 C++ 的函数
    int main()
    {
    	//初始化全局L
    	lua_State *L = luaL_newstate();
    
    	//打开库
    	luaL_openlibs(L);
    
    	//载入lua脚本文件
    	if (luaL_loadfile(L,"LuaFile\lua.lua"))   // lua.lua 的路径,这里使用相对路径
    	{
    		printf("file load error
    ");
    	}
    
    	lua_pcall(L,0,0,0);    // 载入 Lua 文件
    
    	lua_pushcfunction(L, c_Print);		// 将C++函数push进来
    	lua_setglobal(L, "c_Print");		// 将C++函数进行注冊。这样lua文件就能识别到了。

    lua_getglobal(L, "l_Print"); lua_pcall(L,0,0,0); // 调用lua 中的 l_Print 函数 lua_close(L); return 0; } /************************************* Lua.lua version = "Lua version: 5.1.5"; function l_Print() c_Print(version); end; print("Load LuaFile Accomplish"); *************************************/


    执行结果:



  • 相关阅读:
    flume sink两种类型 file_rool 自定义sing com.mycomm.MySink even if there is only one event, the event has to be sent in an array
    为什么引入进程20年后,又引入线程?
    As of Flume 1.4.0, Avro is the default RPC protocol.
    Google Protocol Buffer 的使用和原理
    Log4j 2
    统一日志 统一订单
    网站行为跟踪 Website Activity Tracking Log Aggregation 日志聚合 In comparison to log-centric systems like Scribe or Flume
    Percolator
    友盟吴磊:移动大数据平台的架构、实践与数据增值
    Twitter的RPC框架Finagle简介
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7130318.html
Copyright © 2011-2022 走看看