VC下的luajit的集成比linux/unix/mac下还要简单,至少我的过程是这样的。
平台:windows7-32bit, VS2013.
1,下载:luajit2.0.2,解压缩。通过vs2013的native tool command promot进入src目录:
然后,很快就编译好了。生成的文件lib啥的,都直接在src目录下。强迫症患者找个好地方放,而随便的人,就在那里了。
2,新建一个VC的console工程,把刚才的src目录加入到工程的include与lib目录中。如果include与lib指向了原生的非jit的lua目录,则会使用原生lua。
3,下面是我的例子程序,包括了一个简单的script.lua脚本。
-
print "hello world"
-
print (package.path)
-
-
function foo()
-
end;
-
-
function callbar()
-
print "call bar"
-
for i=0,30000000 do
-
bar()
-
end
-
end
还有一个C++文件,演示了从lua调用C++函数还有从C++调用lua函数。
-
// hellolua.cpp : Defines the entry point for the console application.
-
//
-
-
#include "stdafx.h"
-
#include "windows.h"
-
-
-
extern "C" {
-
#include "lua.h"
-
#include "lualib.h"
-
#include "lauxlib.h"
-
}
-
-
#pragma comment(lib, "lua51.lib")
-
//#pragma comment(lib, "lua5.1.lib")
-
-
static int l_bar(lua_State *L) {
-
return 0;
-
}
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
int status;
-
lua_State * L;
-
-
L = luaL_newstate();
-
-
luaL_openlibs(L);
-
-
status = luaL_loadfile(L, "script.lua");
-
if (status)
-
{
-
printf("could not open lua file : %s\n", lua_tostring(L, -1));
-
return -1;
-
}
-
-
/* do the file */
-
printf("run script\n");
-
int result = lua_pcall(L, 0, 0, 0);
-
-
if (result) {
-
printf("failed to run script : %s\n", lua_tostring(L, -1));
-
return -1;
-
}
-
-
printf("run foo\n");
-
-
DWORD tm1 = GetTickCount();
-
for (int i = 0; i < 30000000; ++i) {
-
lua_getglobal(L, "foo");
-
result = lua_pcall(L, 0, 0, 0);
-
if (result) {
-
printf("failed to run script : %s\n", lua_tostring(L, -1));
-
return -1;
-
}
-
}
-
-
printf("run time is %d-%d\n",tm1, GetTickCount()-tm1);
-
-
/* call from lua by c */
-
lua_pushcfunction(L, l_bar);
-
lua_setglobal(L, "bar");
-
if (status) {
-
printf("register function failed : %s\n", lua_tostring(L, -1));
-
return -1;
-
}
-
-
lua_getglobal(L, "callbar");
-
tm1 = GetTickCount();
-
status = lua_pcall(L, 0, 0, 0);
-
if (status) {
-
printf("callbar failed: %s\n", lua_tostring(L, -1));
-
return -1;
-
}
-
-
printf("run time is %d-%d\n", tm1, GetTickCount() - tm1);
-
-
lua_close(L);
-
return 0;
-
}
阅读(2995) | 评论(0) | 转发(0) |