lua拓展中包含第三方库
之前在利用codeblock建立了lua的拓展,但仅仅是封装了几个简单的函数。我当前需要在扩展中调用第三方库的函数实现,因此进一步进行测试。
一、包含mci库
新建dll工程,利用之前的方法建立lua拓展,然后按照链接第三方库的方式包含mci的lib,删除工程中原有的main.h和main.cpp,新建exmcitest.c如下:
| 1 |  | 
lua文件如下:
| 1 |  | 
可以通过编译,但是使用test_lua进行测试的时候,结果只打印了test0,说明没有链接到mci的库。
对上面的.c文件进行修改,只留下printf("1");其余的都注释掉,发现可以打印出1.
二、寻因
猜想1.因为mci库没有lua接口
刚开始都没有链接到对应的库,猜测是因为lua脚本在解析CreatMci函数时需要去调用mci库中的函数,但是mci库没有设置lua的调用接口,所以失败。
现在尝试建立两个都包含lua接口的库1和2,用2包含1再次进行尝试。
- 新建0612test1工程,构建选项中添加liblua.a的库以及src搜索目录,将。 - 工程代码如下: - 0612test1.h - 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- #ifndef __0612TEST1_H__
 #define __0612TEST1_H__
 #include <windows.h>
 /* To use this exported function of dll, include this header
 * in your project.
 */
 int extest();
 #ifdef BUILD_DLL
 #define DLL_EXPORT __declspec(dllexport)
 #else
 #define DLL_EXPORT __declspec(dllimport)
 #endif
 #ifdef __cplusplus
 extern "C"
 {
 #endif
 //void DLL_EXPORT SomeFunction(const LPCSTR sometext);
 #ifdef __cplusplus
 }
 #endif
 #endif // __0612TEST1_H__- 0612test1.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- #include "stdio.h"
 #include "0612test1.h"
 #define LUA_LIB
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
 #include "luaconf.h"
 int extest()
 {
 printf("ex1");
 return 0;
 }
 static int test1(lua_State *L)
 {
 printf("1");
 return 0;
 }
 LUALIB_API int luaopen_0612test1(lua_State *L)
 {
 printf("Hello__1\n");
 lua_register(L, "test1", test1);
 return 1;
 }
- 新建0612test2工程,构建选项中添加liblua.a、lib0612test1.a的库以及src搜索目录。并把0612test1.h复制到工程文件夹下,将0612test1.dll复制到debug文件夹。 - 工程代码如下: - 0612test2.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
 38
 39
 40- #include "windows.h"
 #include "stdio.h"
 #define LUA_LIB
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
 #include "luaconf.h"
 #include "0612test1.h"
 static int test2(lua_State *L)
 {
 printf("2");
 extest();
 return 0;
 }
 static int HelloFun(lua_State *L)
 {
 printf("HelloWorldlw");
 //test1();//error
 return 0;
 }
 static int HaHa(lua_State *L)
 {
 printf("HaHa");
 return 0;
 }
 LUALIB_API int luaopen_0612test2(lua_State *L)
 {
 printf("Hello__2\n");
 lua_register(L, "HelloFun", HelloFun);
 lua_register(L, "test2", test2);
 return 1;
 }
- 新建测试工程0612testlua工程,包含libluadll.a、lib0612test1.a、lib0612test2.a库,设置src为搜索路径,将0612test1.dll、0612test2.dll和luadll.dll复制到debug文件夹。 - main.cpp - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19- #include <stdio.h>
 extern "C"
 {
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
 #include "luaconf.h"
 };
 int main(int argc, char* argv[])
 {
 lua_State* L = luaL_newstate();
 luaL_openlibs(L);
 luaL_dofile(L, "D:\\CodeBlocks\\workspace\\0612test\\0612testlua\\bin\\Debug\\0612test.lua");
 lua_close(L);
 return 0;
 }- 0612test.lua - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10- print("test0")
 local ans = require("0612test2")
 print(ans)
 print("test1")
 HelloFun()
 print("test2")
 test2()
 print("test3")
 test1()
 print("test4")- 结果为: - 1 
 2
 3
 4
 5
 6- test0
 Hello__2
 D:\CodeBlocks\workspace\0612test\0612testlua\bin\Debug\0612test2.dll
 test1
 HelloWorldlwtest2
 2ex1test3- emmm - 结果好像和test1是否含有lua接口关系不大……毕竟实现的是不带lua接口的函数。此处留个疑问,之后有时间进行补充测试。