使用BabeLua在VS中创建Lua项目

转自:https://blog.csdn.net/qq_21031727/article/details/79498606

如果你的VS安装了BabeLua,那么就能在我写的另一篇VS2015中编译lua.iib并运行lua程序后,接着在工程下新建Lua项目了。取名myLuaCode。 

然后右击,设为启动项目,此时应该是如图所示为空项目。 
这里写图片描述 
然后再右击项目,将我之前生成的myLuaTest.exe填好,工作目录也设置为F:\myLuaProj\myLuaCode。注意我之前以为能随便设置,后来发现如果随便设置的话,VS会另外拷贝一份lua代码到项目目录下,导致出现2份Lua代码很乱,所以最好将工作目录设置为项目目录。下图的Working path换成项目目录下,如F:\myLuaProj\myLuaCode。 
这里写图片描述
这是导入myLuaCode文件夹,因为之前名字取得不好,所以出现了2个父子同名文件夹F:\myLuaProj\myLuaCode\myLuaCode,这里也懒得改了: 
这里写图片描述

注意我之前新建的myLuaTest.exe里面的代码如下图所示:

#include <lua.hpp>
#include <windows.h>
int main() {

    lua_State*l = luaL_newstate();
    luaL_openlibs(l);
    luaL_dofile(l, "myLuaCode/main.lua");
    lua_close(l);
    system("pause");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

所以生成的exe会在上面所说的F:\myLuaProj\myLuaCode目录下查找myLuaCode/main.lua这个文件。 
如果在这个目录下我们创一个main.lua文件,那么就可以成功运行了。 
如果想建几个Lua文件也是没问题的了。如下图所示,做个lua代码练习。 
这里写图片描述
test.lua代码如下:

test = {}
test.str = "this is a string"
function test.func1()
    print("这是一个公有函数")
end
local function func2()
    print("这是一个私有函数")
end
function test.func3()
    func2()
end
return test
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

main.lua代码如下:

print("hello main")
local test= require("myLuaCode.test")
print(test.str)
test.func1()
test.func3()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

以下是输出结果。 
这里写图片描述

注意出现了中文字符输出乱码,需要挨个单击.lua文件,然后如下图所示,设置成简体中文保存。 
这里写图片描述

如果想更加合理的管理各个项目间的关系,有时候需要更改项目结构。 
比如想要更改lua文件搜索路径的话,可以参考这里

猜你喜欢

转载自blog.csdn.net/menghuangxiao/article/details/80459640