在C++中使用Lua

1. 下载Lua源码

Lua源码下载地址 http://www.lua.org/download.html

2. 创建Lua静态库

在vs2008中创建一个静态库项目(我创建的叫LuaLib),注意:一定要取消“预编译头”选项;(否则会报一大堆有关stdafx.h的错误,也可以稍后自行更改设置)

建成后将Lua源码的.h和.c加入该项目

进入项目属性,修改编译输出,方便其他项目使用lib库

配置属性->常规->输出目录,设置为“$(SolutionDir)\lib\”

配置属性->管理员->常规->输出文件,设置为“$(OutDir)\$(ProjectName)_d.lib”

编译,有一些警告,可以不用管

在Lualib文件夹下的lib文件中有输出的lualib_d.lib

3. 创建Test

在LuaLib解决方案中新建Win32控制台程序testLua

4. 引入前面生产的Lualib库

进入testLua项目属性

配置属性->C/C++->常规->附加包含目录,设置为“..\..\src”

配置属性->链接器->常规->附加库目录,设置为“..\lib\”

配置属性->链接器->输入->附加依赖项,设置为“Lualib_d.lib”

同时还需要将Lua源码中的以下几个头文件放在lib文件夹中,方便项目引用

lauxlib.h

lua.h

luaconf.h

lualib.h

5. 编写C++代码

#include "stdafx.h"

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. extern "C"
  5. {
  6. #include "lua.h"
  7. #include "lualib.h"
  8. #include "lauxlib.h"
  9. }
  10. int fcAdd(lua_State *lu)
  11. {
  12. int a = lua_tointeger(lu, 1);
  13. int b = lua_tointeger(lu, 2);
  14. lua_pushnumber(lu, a+b); //结果压栈
  15. return 1; //返回1个结果
  16. }
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. int nret = 0;
  20. lua_State *lu = luaL_newstate();
  21. luaL_openlibs(lu);
  22. //栈操作
  23. lua_pushinteger(lu, 2);
  24. lua_pushinteger(lu, 3);
  25. int n = lua_tointeger(lu, 1);
  26. n = lua_tointeger(lu, 2);
  27. int nStack = lua_gettop(lu);
  28. lua_pop(lu, 2);
  29. nStack = lua_gettop(lu);
  30. //执行内存脚本
  31. string str = "print (\"Hello world!\")";
  32. luaL_loadbuffer(lu, str.c_str(), str.length(), "line");
  33. lua_pcall(lu, 0,0,0);
  34. //加载脚本中定义的变量
  35. nret = luaL_dofile(lu, "..\\scripts\\test.lua");
  36. lua_getglobal(lu, "aa");
  37. lua_getglobal(lu, "bb");
  38. int bb = lua_tointeger(lu, -1);
  39. int aa = lua_tointeger(lu, -2);
  40. //执行脚本中定义的无参函数
  41. lua_getglobal(lu, "hello");
  42. nret = lua_pcall(lu, 0,0,0);
  43. //执行脚本中定义的有参函数
  44. lua_getglobal(lu, "fadd");
  45. lua_pushnumber(lu, aa);
  46. lua_pushnumber(lu, bb);
  47. nret = lua_pcall(lu, 2,1,0);
  48. if (nret != 0)
  49. {
  50. const char *pc = lua_tostring(lu, -1);
  51. cout << pc;
  52. }
  53. else
  54. {
  55. nret = lua_tointeger(lu, -1);
  56. cout << "调用脚本函数:" << endl;
  57. cout << aa << " + " << bb << " = " << nret << endl;
  58. lua_pop(lu, 1);
  59. }
  60. //脚本中调用C++函数
  61. lua_pushcfunction(lu, fcAdd);
  62. lua_setglobal(lu, "fcAdd");
  63. lua_getglobal(lu, "fc");
  64. lua_pushnumber(lu, aa);
  65. lua_pushnumber(lu, bb);
  66. nret = lua_pcall(lu, 2,1,0);
  67. if (nret != 0)
  68. {
  69. const char *pc = lua_tostring(lu, -1);
  70. cout << pc;
  71. }
  72. else
  73. {
  74. nret = lua_tointeger(lu, -1);
  75. cout << "调用C++函数:" << endl;
  76. cout << aa << " + " << bb << " = " << nret << endl;
  77. lua_pop(lu, 1);
  78. }
  79. lua_close(lu);
  80. std::system("pause");
  81. return 0;
  82. }

6. Lua脚本代码

在LuaLib文件夹下创建scripts文件夹,在其中创建test.lua,代码如下:

aa=2;

  1. bb=3;
  2. function hello(a,b)
  3. print ("Hello in script!")
  4. end
  5. function fadd(a,b)
  6. return a+b
  7. end
  8. function fc(a,b)
  9. return fcAdd(a,b)
  10. end