zoukankan      html  css  js  c++  java
  • c++调用lua

    VC要编译LUA文件必须先配置VC编程环境。。我用的是VC6.0,lua 5.1.4版

    首先将lua的"lua.h"  ,"lualib.h", "lauxlib.h" 这三个放在vc程序include文件夹下

    然后将lua的lua5.1.lib放在lib文件夹下就OK了

    下面看看我写的一个小例子:

    a.cpp

    [cpp] view plain copy
     
    1. #include "windows.h"     
    2.     
    3. extern "C"{     
    4. #include "lua.h"       
    5. #include "lualib.h"     
    6. #include "lauxlib.h"      
    7. }    
    8.     
    9.     
    10. #pragma   comment(lib,"lua5.1.lib")     
    11.     
    12.     
    13. lua_State * L;    
    14.     
    15. static int clib(lua_State *L)   //给lua调用的c函数必须定义成static int XXX(lua_State *L)     
    16. {    
    17.     char   path[MAX_PATH];     
    18.     GetCurrentDirectory(MAX_PATH,path);    
    19.     lua_pushstring(L,path);    
    20.     return 1;   //为什么要返回1?这是有依据的,该函数把结果压入了栈,lua调用该函数将从栈中     
    21.     //取1个结果     
    22. }    
    23.     
    24.     
    25. int main ( int argc, char *argv[] )    
    26. {    
    27.     int sum;    
    28.     //创建一个指向lua解释器的指针     
    29.     L = luaL_newstate();    
    30.     //加载lua标准库     
    31.     luaL_openlibs(L);    
    32.     //注册C++函数     
    33.     lua_register(L,"clib",clib);    
    34.     //加载脚本     
    35.     luaL_dofile(L,"4.lua");    
    36.     //调用函数     
    37.     lua_getglobal(L,"run");    
    38.     //运行函数并把结果压入栈     
    39.     lua_pcall(L,0,0,0);    
    40.     //关闭并释放资源     
    41.     lua_close(L);    
    42.     return 0;    
    43. }    


    LUA文件 

    [cpp] view plain copy
     
      1. function run()    
      2.      print("call running from c")    
      3.      print(clib())    
      4. end    

    http://blog.csdn.net/chinazhd/article/details/7772316

  • 相关阅读:
    UVa 10118 记忆化搜索 Free Candies
    CodeForces 568B DP Symmetric and Transitive
    UVa 11695 树的直径 Flight Planning
    UVa 10934 DP Dropping water balloons
    CodeForces 543D 树形DP Road Improvement
    CodeForces 570E DP Pig and Palindromes
    HDU 5396 区间DP 数学 Expression
    HDU 5402 模拟 构造 Travelling Salesman Problem
    HDU 5399 数学 Too Simple
    CodeForces 567F DP Mausoleum
  • 原文地址:https://www.cnblogs.com/findumars/p/5402243.html
Copyright © 2011-2022 走看看