Lua Study Steps 1 run lua script file

来源:互联网 发布:网络购物投诉电话 编辑:程序博客网 时间:2024/06/07 05:02

Lua version 5.1

Lua是一个被设计成用于扩展C/C++编程的轻量级脚本语言.
1. http://www.lua.org/home.html download
   http://luaforge.net/frs/?group_id=110 binaries

Add include(source file) directory and library(*.dll and *.lib) directory in VC.
Look luavs.bat file for install, you can get luaXX.dll/XX.lib and lua.exe luac.exe.
Add the path to environment variables that you can run lua any path

2. first program
lua_open()返回一个指向Lua解释器的一个指针。
luaL_openlibs()用于装载Lua库,它提供了一些简单的函数,如:print。
通过调用luaL_dofile()执行脚本。它的作用是读取并解释脚本。
最后,通过lua_close()函数关闭Lua。

In Visual C++ you’ll need to follow these steps:
Create a new empty Win32 Console Application Project.
如果你直接使用C而不是C++,将文件名改为luatest.c,然后将extern "C"删除。
put *.lua to your *.cpp path or you need location absolute path for *.lua file

 

#include //for _T macro#include #include #include using namespace std;#include "cpp_utils.h"//for GetModuleFileName use#include #pragma comment( lib ,"shlwapi.lib")//=========lua partextern "C"{#include #include #include }//load lib#pragma comment( lib ,"lua51.lib")//=========lua part endint main(){/* the Lua interpreter */lua_State* L;// /* initialize Lua */ L = lua_open();/* load Lua base libraries */luaL_openlibs(L);/*location app path */TCHAR path[MAX_PATH];GetModuleFileName(NULL, path, MAX_PATH);tstring str = path;str = str.substr(0, str.rfind( _T("//") ) );str += _T("//hello.lua");if ( FALSE == PathFileExists( str.c_str() ) ){cout<<"False";}string stra = widen2narrow(str);/* run the script */luaL_dofile(L, stra.c_str());/* cleanup Lua */ lua_close(L);system("pause");return 0;}

 

原创粉丝点击