c++调用lua:
#include "stdafx.h"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#pragma comment(lib, "lua5.1.lib")
///lua 解释器指针
lua_State* L = nullptr;
int luaAdd(int x, int y)
{
lua_getglobal(L, "add");
int nTop = lua_gettop(L);
lua_pushnumber(L, x);
nTop = lua_gettop(L);
lua_pushnumber(L, y);
nTop = lua_gettop(L);
lua_call(L, 2, 1);
nTop = lua_gettop(L);
int sum = (int)lua_tonumber(L, -1);
nTop = lua_gettop(L);
lua_pop(L, 1);
return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
L = lua_open();
luaopen_base(L);
luaL_openlibs(L);
luaL_dofile(L, "add.lua");
int sum = luaAdd(10, 15);
printf("%d", sum);
lua_close(L);
system("pause");
return 0;
}