Chinaunix首页 | 论坛 | 博客
  • 博客访问: 504286
  • 博文数量: 118
  • 博客积分: 2575
  • 博客等级: 大尉
  • 技术积分: 1263
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-27 09:37
文章分类

全部博文(118)

文章存档

2017年(11)

2016年(8)

2015年(1)

2014年(9)

2013年(7)

2012年(38)

2011年(14)

2010年(18)

2009年(12)

分类: Windows平台

2014-07-01 14:51:05

VC下的luajit的集成比linux/unix/mac下还要简单,至少我的过程是这样的。

平台:windows7-32bit, VS2013.

1,下载:luajit2.0.2,解压缩。通过vs2013的native tool command promot进入src目录:

  1. msvcbuild
然后,很快就编译好了。生成的文件lib啥的,都直接在src目录下。强迫症患者找个好地方放,而随便的人,就在那里了。

2,新建一个VC的console工程,把刚才的src目录加入到工程的include与lib目录中。如果include与lib指向了原生的非jit的lua目录,则会使用原生lua。

3,下面是我的例子程序,包括了一个简单的script.lua脚本。

点击(此处)折叠或打开

  1. print "hello world"
  2. print (package.path)

  3. function foo()
  4. end;

  5. function callbar()
  6.     print "call bar"
  7.     for i=0,30000000 do
  8.         bar()
  9.     end
  10. end
还有一个C++文件,演示了从lua调用C++函数还有从C++调用lua函数。

点击(此处)折叠或打开

  1. // hellolua.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include "windows.h"


  5. extern "C" {
  6. #include "lua.h"
  7. #include "lualib.h"
  8. #include "lauxlib.h"
  9. }

  10. #pragma comment(lib, "lua51.lib")
  11. //#pragma comment(lib, "lua5.1.lib")

  12. static int l_bar(lua_State *L) {
  13. return 0;
  14. }

  15. int _tmain(int argc, _TCHAR* argv[])
  16. {    
  17.     int status;
  18.     lua_State * L;

  19.     L = luaL_newstate();

  20.     luaL_openlibs(L);

  21.     status = luaL_loadfile(L, "script.lua");
  22.     if (status)
  23.     {
  24.         printf("could not open lua file : %s\n", lua_tostring(L, -1));
  25.         return -1;
  26.     }

  27.     /* do the file */
  28.     printf("run script\n");
  29.     int result = lua_pcall(L, 0, 0, 0);

  30.     if (result) {
  31.         printf("failed to run script : %s\n", lua_tostring(L, -1));
  32.         return -1;
  33.     }

  34.     printf("run foo\n");
  35.     
  36.     DWORD tm1 = GetTickCount();
  37.     for (int i = 0; i < 30000000; ++i) {
  38.         lua_getglobal(L, "foo");
  39.         result = lua_pcall(L, 0, 0, 0);
  40.         if (result) {
  41.             printf("failed to run script : %s\n", lua_tostring(L, -1));
  42.             return -1;
  43.         }
  44.     }

  45.     printf("run time is %d-%d\n",tm1, GetTickCount()-tm1);
  46.     
  47.     /* call from lua by c */
  48.     lua_pushcfunction(L, l_bar);
  49.     lua_setglobal(L, "bar");
  50.     if (status) {
  51.         printf("register function failed : %s\n", lua_tostring(L, -1));
  52.         return -1;
  53.     }

  54.     lua_getglobal(L, "callbar");
  55.     tm1 = GetTickCount();
  56.     status = lua_pcall(L, 0, 0, 0);
  57.     if (status) {
  58.         printf("callbar failed: %s\n", lua_tostring(L, -1));
  59.         return -1;
  60.     }

  61.     printf("run time is %d-%d\n", tm1, GetTickCount() - tm1);

  62.     lua_close(L);
  63.     return 0;
  64. }









阅读(2813) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~