First of all configure the environment, Baidu Library this talk in detail
Http://wenku.baidu.com/view/7912da3667ec102de2bd8957.html
After the environment was configured, I also wrote a simple call
Main.cpp
#include <stdio.h>
extern "C" {
#include "Lua.h"
#include "Lualib.h"
#include "Lauxlib.h"
};
Lua_state *l;
int luaadd (int x, int y)
{
int sum;
Lua_getglobal (L, "add");
Lua_pushnumber (L, x);
Lua_pushnumber (L, y);
Lua_call (L, 2, 1);
sum = (int) lua_tonumber (L,-1);
Lua_pop (L, 1);
return sum;
}
int main (int argc, char *argv[])
{
int sum = 0;
L = Lua_open ();
Luaopen_base (L);
Lual_openlibs (L);
Lual_loadfile (L, "Add.lua");
Lua_pcall (L, 0, Lua_multret, 0);
sum = Luaadd (10, 15);
printf ("The sum is%d\n", sum);
Lua_close (L);
return 0;
}
Add.lua
1--simple example, only do integer addition
2 function Add (x, y)
3 return x + y
4 End
When there is no problem after compiling, there are some problems with the execution, as follows:
Panic:unprotected error in call to Lua APIs (attempt to call a nil value)
Results Google a bit, said is Lual_loadfile replaced Lual_dofile, at that time I used is lual_dofile, this is a possible reason, but not mine.
Finally found the reason, Add.lua placed in the wrong place, I put it in the VS. cpp files in the same directory, and then put into the engineering directory inside.
Available Luaide Downloads: http://www.blueantstudio.net/content/index_static.php
Note: Content reproduced from: http://www.cnblogs.com/good90/archive/2012/08/19/2645997.html