lua封装mci库
之前的方法都是针对于本身就有lua接口的库来说的,对于一般的库,可以通过在lua源代码中增加接口的方式实现封装。
构建libluawithmci.a
- 在lua源代码的src文件夹中添加lmci.c文件如下:
| 1 |  | 
- 新建luawithmci的lib工程,移除源文件,将src文件夹中的文件添加到工程中。
- 工程包含mci.lib,并把包含mci.h的文件路径添加到搜索路径中。
- 编译,生成libluawithmci.a。
测试工程
- 新建testmci工程,包含libluawithmci.a和mci.lib,并把包含mci.h的文件路径添加到搜索路径中。 
- main.cpp如下: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21- #include <iostream>
 #include <windows.h>
 using namespace std;
 extern "C" {
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
 #include "luaconf.h"
 }
 int main()
 {
 cout << "Hello world!" << endl;
 lua_State* L = luaL_newstate();
 luaL_openlibs(L);
 luaL_dofile(L, "D:\\CodeBlocks\\workspace\\0610test\\testmci\\bin\\Debug\\tst.lua");
 lua_close(L);
 return 0;
 }- 其中,tst.lua如下: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19- function show()
 local b = {}
 local index
 for index = 1,10,1 do
 print(index)
 end
 end
 print("test0")
 show()
 print("test1")
 require("mci")
 print("test2")
 mci.mc_connect()
 print("test3")
 mci.mc_set_mode_auto()
 print("test4")
 mci.mc_moveref()
 print("test5")- 结果为: - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19- Hello world!
 test0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 test1
 test2
 nci_init ok!
 test3
 test4
 moveref
 test5