关闭

lua call c/c++ function

574人阅读 评论(0) 收藏 举报
分类:

this article is tenchnique reminder

key point:

1. c/c++ DLL

2. below mylib as exported name required and called from lua script, should be same

extern "C"

{

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

 

int lhsadd(lua_State *L)

{

return 1;
}

 

int lhssubs(lua_State *L)

{

return 1;
}

static luaL_Reg libs[] = {{"lhsadd",lhsadd},{"lhssubs",lhssubs},{NULL,NULL}};

 

 

__declspec(dllexport) in luaopen_mylib(lua_State *L)

{

const char* libname = "mylib2";

luaL_register(L,libname,libs);

return 1;

}

 

}

 

 

.lua testing script

require "mylib"

print(mylib2.lhsadd(1.1,2.2))

print(mylib2.lhsadd(2.2,1.1))

 

 

 

 

 

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19797次
    • 积分:335
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:38篇
    • 译文:0篇
    • 评论:0条
    文章分类