lua和c++相互调用

//#include <signal.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
#include <iostream>
extern "C"{
 #include "lua.h"
 #include "lauxlib.h"
 #include "lualib.h"
}
lua_State *L;

int luaadd(lua_State* L,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;

}
extern int CL_DomyLua(lua_State *L);   
extern int CL_Drawtext(lua_State *L);   

static luaL_reg pRegisterFuc[] =
{
 {"DomyLua",    CL_DomyLua},    
 {"Drawtext",   CL_Drawtext},    
 {NULL,     NULL}
};
int CL_DomyLua(lua_State* L)
{
 return 0;
}
int CL_Drawtext(lua_State* L)
{
 return 0;
}

int main(){
 std::cout<<"aaa"<<std::endl;
 L = lua_open();
 luaL_openlibs(L);

 for(int i=0;; ++i)
 {
  if(NULL == pRegisterFuc[i].name) break;
  lua_register(L, pRegisterFuc[i].name, pRegisterFuc[i].func);
 }

 luaL_dofile(L,"test.lua");
 int sum = luaadd(L,5,7);
 std::cout<<sum<<std::endl;


 system("pause");
 return 0;
}

 

你可能感兴趣的:(C++,c,null,lua)