将c++函数导出到lua脚本中

#include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" int PrintStringList (lua_State * L){ int iStringCount=lua_gettop(L); int i; for(i=1 ; i<=iStringCount ; i++){ if(!lua_isstring(L,1)){ lua_error(L,"Invalid string."); }else{ printf("/t"); printf(lua_tostring(L,i)); printf("/n"); } } return 0; } int main(int argc, char *argv[]) { int s=0; lua_State *L = lua_open(); //load the libs luaL_openlibs(L); lua_register(L,"PrintStringList",PrintStringList); //run a lua script here luaL_dofile(L,"foo.lua"); //printf("/n I am done with Lua in C++/n"); lua_close(L); }

 

foo.lua:

FullName ="Alex";
LastName = "Michael";
CompanyName = "casia";
PrintStringList(FullName,LastName,CompanyName);

 

你可能感兴趣的:(将c++函数导出到lua脚本中)