#include <windows.h>
#include <iostream>
using namespace std;
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <iostream>
#pragma comment(lib,"E:\\luaSRC\\lua5.1\\lib\\static\\lua5.1.lib")
//1.lua
/*
function f (x, y)
--return (x^2 * math.sin(y))/(1 - x)
return x+y
end
*/
/* call a function `f' defined in Lua */
double f (lua_State* L, double x, double y)
{
double z;
luaL_dofile( L, "1.lua" );//书上的例子 用的luaL_loadfile 又没有pcall(0,0,0) 导致错误 找了半天
/* push functions and arguments */
lua_getglobal(L, "f"); /* function to be called */
lua_pushnumber(L, x); /* push 1st argument */
lua_pushnumber(L, y); /* push 2nd argument */
/* do the call (2 arguments, 1 result) */
if (lua_pcall(L, 2, 1, 0) != 0)
luaL_error(L, "error running function `f': %s",
lua_tostring(L, -1));
/* retrieve result */
if (!lua_isnumber(L, -1))
luaL_error(L, "function `f' must return a number");
z = lua_tonumber(L, -1);
lua_pop(L, 1); /* pop returned value */
return z;
}
void main()
{
lua_State* L = lua_open();
luaL_openlibs(L);
cout<<f(L, 1, 5);
}
lua程序设计里面的调用Lua函数的小例子 有个错误被我找到了
最新推荐文章于 2024-04-23 13:40:50 发布