First step in lua 1

1. Create a win32 console application.

2. Set the include directory.

3. Set the Lib directory

4. keep an copy of lua5.1.dll in the project folder.

 

Call an function in lua

Call C++ function in lua

 

 

#include "stdafx.h"
#include <stdio.h>
extern "C" {
#include "lua.h" 
#include "lualib.h"
#include "lauxlib.h"
}
/* Lua interpreter  */
lua_State* L;

int luaadd ( int x, int y )

{

int sum;

/*Get the function name */

lua_getglobal(L, "add");

/*Pass the first argument */

lua_pushnumber(L, x);

/* Pass second argument */

lua_pushnumber(L, y);

/* call the function with 2

arguments, return 1 result */

lua_call(L, 2, 1);

/* get the result */

sum = (int)lua_tonumber(L, -1);

lua_pop(L, 1);

return sum;

}

 int GetArrage(lua_State *L)
{
  int n = lua_gettop(L);
  float sum = 0;
  int i;
  for(i=0; i<=n;++i)
  {
    sum += lua_tonumber(L,i);
  }
  lua_pushnumber(L,sum/n);
  lua_pushnumber(L,sum);

  return 2;

}


int main ( int argc, char *argv[] )
{
  /* Initial Lua */ 
  L = lua_open(); 
  /* load Lua base lib*/ 
  luaL_openlibs(L);
  lua_register(L,"arr", GetArrage);
  /* run script */
  luaL_dofile(L, "test.lua");
  int result = luaadd(1,2);
  printf("The result is %d /n", result);

  /* clear Lua */ 
  lua_close(L);

  printf( "Press enter to exit…" ); 
  getchar();
  return 0;
}

 

 

lua:

function add(x,y)

a1, a2 = arr(12,32,54,33)
print ("Sum is ", a2)
print ("arrage is ", a1)
return x+y

end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值