64,651
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <assert.h>
#include <exception>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/*Lua解释器指针*/
lua_State* L;
int main ( int argc, char *argv[] )
{
L = luaL_newstate();
luaL_openlibs(L);
int iError = luaL_dofile(L, "test.lua");
lua_close(L);
int a = 11 ;
int b = 12 ;
(lua_getglobal(L,"add"));
if (lua_pcall(L, 0, 0, 0))
{
printf("Error calling lua function error");
}
lua_pushinteger(L,a) ;
lua_pushinteger(L,b) ;
int ret = lua_pcall(L,2,1,0) ;
if ( ret != 0 )
return 0;
printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(L,-1)) ;
lua_pop(L,1);
/* 清除Lua */
lua_close(L);
getchar();
return 0;
}
--变量定义
width=1 ;
height=2 ;
--lua函数定义,实现加法
function sum(a,b)
return a+b ;
end
#include <stdio.h>
#include <assert.h>
#include <exception>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/*Lua解释器指针*/
lua_State* L;
int main ( int argc, char *argv[] )
{
try
{
L = luaL_newstate();
luaL_openlibs(L);
int iError = luaL_dofile(L, "test.lua");
int a = 11 ;
int b = 12 ;
(lua_getglobal(L,"add"));
if (lua_pcall(L, 0, 0, 0))
{
printf("Error calling lua function error");
}
lua_pushinteger(L,a) ;
lua_pushinteger(L,b) ;
int ret = lua_pcall(L,2,1,0) ;
if ( ret != 0 )
return 0;
printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(L,-1)) ;
lua_pop(L,1);
/* 清除Lua */
lua_close(L);
}
catch (...)
{
printf("Error calling lua function error");
}
getchar();
return 0;
}
if (lua_pcall(L, 0, 0, 0))
{
printf("Error calling lua function error");
}
看看错误是不是这造成的:
lua_pcall
如果是那就是你lua_close(L);造成的错误