注册全局环境C函数给lua

转载请注明出处:http://jonee.net/2012/06/14/137.html


#include "windows.h"     

#include <iostream.h>   

extern "C"{        

#include "lua.h"         

#include "lualib.h"       

#include "lauxlib.h"        

}       

#pragma   comment(lib,"lua5.1.lib")       

lua_State * L;     

static int foo (lua_State *L) {  

    const char *buff = luaL_checkstring(L, -1);   

    printf(buff);  

    return 0;                   /* number of results */  

}  

int main()  

{  

    //创建一个指向lua解释器的指针       

    L = luaL_newstate();       

    //加载lua标准库       

    luaL_openlibs(L);       

    //注册C++函数       

    lua_register(L,"foo",foo);         

    luaL_dofile(L,"1.lua");            

    lua_close(L);     

    return 0;      

} 

lua文件:


foo("this is from lua!/n") 

留下只言片语: