1.首先下载基于VS的LUA源码,地址为http://luabinaries.luaforge.net/download.html
2.在LIB工程中添加mylib.c文件
3.在文件当中添加如下的代码
- #include "lua.h"
- #include "lauxlib.h"
- #include "lualib.h"
- static int myfun(lua_State* L)
- {
- printf("myfun is succeed!");
- return 0;
- }
- static const struct luaL_Reg mylib[] =
- {
- {"myfun",myfun},
- {NULL,NULL}
- };
- LUALIB_API int luaopen_mylib(lua_State* L)
- {
- luaL_register(L,"mylib",mylib);
- }
4。在lualib.h中增加如下定义
- #define LUA_MYLIBNAME "mylib"
- LUALIB_API int (luaopen_mylib) (lua_State *L);
5.在linit.c,中将
- static const luaL_Reg lualibs[] = {
- {"", luaopen_base},
- {LUA_LOADLIBNAME, luaopen_package},
- {LUA_TABLIBNAME, luaopen_table},
- {LUA_IOLIBNAME, luaopen_io},
- {LUA_OSLIBNAME, luaopen_os},
- {LUA_STRLIBNAME, luaopen_string},
- {LUA_MATHLIBNAME, luaopen_math},
- {LUA_DBLIBNAME, luaopen_debug},
- {LUA_MYLIBNAME,luaopen_mylib},
- {NULL, NULL}
- };
最后增加一行,如上图所示
6.更新include目录的lualib.h文件
7.测试如下
- #include "stdafx.h"
- extern "C" {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- int main()
- {
- lua_State *L = luaL_newstate();
- lua_cpcall(L, luaopen_base, 0);
- lua_cpcall(L, luaopen_io, 0);
- lua_cpcall(L, luaopen_string, 0);
- lua_cpcall(L,luaopen_mylib,0);
- const char *buf = "mylib.myfun()";//注意调用规则
- //luaL_loadstring(L, buf);
- int s = luaL_loadstring(L, buf);
- if ( s==0 )
- {
- s = lua_pcall(L, 0, LUA_MULTRET, 0);
- }
- lua_close(L);
- getchar();
- return 0;
- }
ok
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。