Lua Study Steps 1 run lua script file

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 part
extern "C"
{
	#include 
  
  
	#include 
  
  
	#include 
  
  
}
//load lib
#pragma comment( lib ,"lua51.lib")
//=========lua part end


int 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值