一、配置环境
1、首先下载lua for windows,我使用的是 Lua 5.1.4
2、在VS2005上配置lua环境
I、添加lua头文件包含目录
项目->属性->配置属性->C/C++->常规->附加包含目录:D:\My Program Files (x86)\Lua\5.1\include(根据你的lua安装路径设置)
II、添加静态库目录
项目->属性->配置属性->连接器->常规->附加库目录:D:\My Program Files (x86)\Lua\5.1\lib(根据你的lua安装路径设置)
III、添加引用库
项目->属性->配置属性->连接器->输入->附加依赖库:lua51.lib
二、编写hello,world
1、新建一个空的win32工程(lua_test),添加一个源文件lua_test.cpp
2、用文本编辑器创建一个lua脚本(hello.lua),内容如下:
print("hello,world!");
将文件置于win32工程目录下(../lua_test/lua_test/)
3、lua_test.cpp源码如下:
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <cstdlib> /* For function exit() */
#include <cstdio> /* For input/output */
void bail(lua_State *L, char *msg)
{
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -1));
exit(1);
}
int main()
{
lua_State *L = luaL_newstate(); /* Create new lua state variable */
/* Load Lua libraries, otherwise, the lua function in *.lua will be nil */
luaL_openlibs(L);
if( luaL_loadfile(L, "hello.lua") ) /* Only load the lua script file */
bail(L, "luaL_loadfile() failed");
if( lua_pcall(L,0,0,0) ) /* Run the loaded lua file */
bail(L, "lua_pcall() failed");
lua_close(L); /* Close the lua state variable */
return 0;
}
4、编译,执行就可以看到输出"hello,world!"了