Lua与C++交互初探之C++调用Lua

来源:互联网 发布:淘宝少女双肩背包 编辑:程序博客网 时间:2024/06/06 00:11

转自:http://www.cnblogs.com/Zackzhang/p/5093329.html


一、lua环境的搭建

  1. 建议去网上下载luaforwindow,这是一款跟众多window系统的软件一样,安装起来简单方便,一路点next就能搞定了。而且他还包含了有用的与lua有关的基本工具

    他包括以下组件:

    Lua(Command Line):lua的一个命令行编辑器。简单轻便,习惯命令行编(zhuang)辑(bi)的可以试试。

    Lua Examples: 包含lua使用的一些例子。

    LuaForWindows Documentation :LuaForWindows这款软件的一些说明

    QuickLuaTour : lua快速入门向导,没什么用,看看就好

    SciTE:lua的一个不错的文本编辑器。可以在里面测试一些lua代码,可以运行测试。前提是要先保存文件在运行,否则他没有任何反应。别问我是怎么知道的,心塞塞

    Documentation:里面包含lua的帮助文档,最有用的就是他了吧。

     

     

    安装好后Lua的环境就算是搭建好了。我们用命令行简单来测试一下:

Ok,木有问题

 

 二、VS环境配置

  1. 这一步是最重要的,一开始我是去lua官网下载的源文件再把他们添加到vs项目,虽然编译是没有问题了,但是在测试运行的时候链接还是出现了问题。很明显我是少了什么东东。后来我改用下面的方法解决了问题。

    1. 选中项目,右键->属性->定位到VC++目录项

    1. 在"可执行文件目录"里添加上lua的安装目录。过程如下:

      定位到Lua的安装文件夹,我的是:

       

      确定后返回

       

       

    2. 用同样的步骤,将"lua安装目录\5.1\include"添加到"包含目录";
    3. 将"lua安装目录\5.1\lib"添加到"库目录";
    4. 跳到"连接器"的"输入"栏。将"附加依赖项"中添加上"lua51.lib;lua5.1.lib";

     

     

    至此环境基本就配置好了。类似下面:

    现在我们用代码测试一遍:

复制代码
#include "stdafx.h"#include <iostream>#include <string.h>using namespace std;extern "C"{#include "lua.h"#include "lauxlib.h"#include "lualib.h"}void main(){    //1.创建一个state    lua_State *L = luaL_newstate();    //2.入栈操作    lua_pushstring(L, "Hello World~");    //3.取值操作    if (lua_isstring(L, 1)) { //判断是否可以转为string        cout << lua_tostring(L, 1) << endl; //转为string并返回    }    //4.关闭state    lua_close(L);    system("pause");    return;} 
复制代码

 

 

是不是木有问题啦╮(╯▽╰)╭

喏,我们亲爱的World君

 

  1. Lua与C++的交互测试

    上面我们已经把需要的环境什么的都配置好了,现在重头戏上场( ̄︶ ̄)

    1. 我们建一个test.lua文件;
    function Communicate(name)return ("Hello "..name..", I`m in Lua");end

     


    1. 我们调用lua文件的cpp代码

    

复制代码
#include "stdafx.h"#include <iostream>  #include <string.h>  using namespace std;extern "C"{#include "lua.h"  #include "lauxlib.h"  #include "lualib.h"  }void main(){    string hello = "This is Zack, I`m in C++";    cout << hello.c_str() << endl;    //创建Lua状态      lua_State *L = luaL_newstate();    if (L == NULL)    {        return;    }    //加载Lua文件      int bRet = luaL_loadfile(L, "test.lua");    if (bRet)    {        cout << "load file error" << endl;        return;    }    //运行Lua文件      bRet = lua_pcall(L, 0, 0, 0);    if (bRet)    {        cout << "pcall error" << endl;        return;    }                                      //读取函数      lua_getglobal(L, "Communicate");        // 获取函数,压入栈中      lua_pushstring(L, "Zack");          // 压入参数      int iRet = lua_pcall(L, 1, 1, 0);// 调用函数,调用完成以后,会将返回值压入栈中,第一个1表示参数个数,第二个1表示返回结果个数。      if (iRet)                       // 调用出错      {        const char *pErrorMsg = lua_tostring(L, -1);        cout << pErrorMsg << endl;        lua_close(L);        return;    }    if (lua_isstring(L, -1))        //取值输出      {        string Result = lua_tostring(L, -1);        cout << Result.c_str() << endl;    }    //关闭state      lua_close(L);    system("pause");    return;}
复制代码

 

结果:

 

 

嗯,大功告成。就酱紫了╰( ̄▽ ̄)╮╭(′▽`)╯╰( ̄▽ ̄)╮


原创粉丝点击