在编写下面代码前首先你的需要#include "lua.h" #include "lualib.h" #include "lauxlib.h"头文件
lua51.lib lua51.dll 这两个库文件
#include <stdio.h>
#include <string.h>extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
#pragma comment(lib, "lua51.lib")//这里是lua的库文件
extern "C" int _Version(lua_State *L)
{
puts("This is Version 1.0 of the Console program");
puts(LUA_VERSION);
puts(LUA_COPYRIGHT);
puts(LUA_AUTHORS);
return 0;
}
static luaL_reg consoleGlua[]=
{
{"Version",_Version},
{NULL,NULL}
};
char gpCommandBuffer[256];
const char *gpCommand(void)
{
printf("Ready> ");
return gets(gpCommandBuffer);
puts("\n");
};
int main()
{
char buff[256];
int error;
lua_State *pLuaState = lua_open();
luaopen_base(pLuaState);
luaopen_base(pLuaState); /* opens the basic library */
luaopen_table(pLuaState); /* opens the table library */
//luaopen_io(pLuaState); /* opens the I/O library */这里一般不开启
luaopen_string(pLuaState); /* opens the string lib. */
luaopen_math(pLuaState); /* opens the math lib. */
luaL_dofile(pLuaState,"test.lua");//加载lua文件
lua_getglobal(pLuaState,"aa");//查找需要调用的函数
lua_pcall(pLuaState,0,0,0);//调用函数
lua_getglobal(pLuaState,"bb");
lua_pcall(pLuaState,0,0,0);
lua_close(pLuaState);//关闭lua文件
getchar();
return 0;
}