最主要是为了使用c++11的里面的一些东西,但这c11的东西又不能直接被lua调用,必须使用c还做 媒人
lua注册文件 lnetReg.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <lua.h> #include <lauxlib.h> #include <stdbool.h>
#define LUA_API_EXPORT __declspec(dllexport)
extern bool Connect(const char* _ip, int _port); static int lconnect(lua_State *L) { size_t sz = 0; const char* ip = luaL_checklstring(L, 1, &sz); int port = luaL_checkinteger(L, 2); bool ret = Connect(ip, port); lua_pushboolean(L, ret); return 1; }
LUA_API_EXPORT int luaopen_network(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "connect", lconnect }, { NULL, NULL }, }; lua_newtable(L); luaL_openlib(L, "network", l, 0); return 1; }
在gameNet.cpp文件中 extern "C" { bool Connect(const char* _ip, int _port) { return CGameNet::GetInstance()->Connect(_ip, _port); } }
|
然后gameNet.cpp 和 gameNet.h 就可以正常使用c++11的东西来写,上面作为一个接口提供个 c 编译
#编写lua dll动态库
编译选项:
1. 配置属性-常规-目标文件扩展名 改成 .dll
2. 配置属性-常规-配置类型 改为 动态库
1 2 3 4 5 6 7 8 9 10
| LUA_API_EXPORT int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion luaL_checkversion(L); #endif luaL_Reg l[] = { { "newproto", lnewproto }, { NULL, NULL }, }; luaL_newlib(L,l);
|
lua中
1 2
| local core = require “sproto_core” core.newproto(bin)
|
#编写lua lib静态库
1 2
| 1. 配置属性-常规-目标文件扩展名 改成 .lib 2. 配置属性-常规-配置类型 改为 静态库
|
在创建luastate的地方声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| extern "C" { int luaopen_srp(lua_State *L); }
lua_State* L = engine->getLuaStack()->getLuaState(); luaopen_srp(L);
LUA_API_EXPORT int luaopen_srp(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "create_verifier", lcreate_verifier }, { NULL, NULL }, }; lua_newtable(L); luaL_openlib(L, "srp", l, 0); return 1; }
|
lua中
1 2
| local srp = require(“srp”) local private_key, public_key = srp.create_client_key()
|
PS: 如果该库中引用到xxx.lib,在需要在引用到这个srp.lib的工程中引入xxx.lib,否则报链接错误2019
a