因为懒得写,所以源码是在这里拷贝过来的,
lua调用dll demo - ff_d - 博客园
这里讲一下配置开发的过程。
上一讲提到了VS2019编译开发lua了,在编译好了之后,我们在这里使用一下。
首先新建一个dll工程,名字随便取,注意和后面的调用保持一致就OK了,我这里就叫“test_luaDll1”,源码内容如下,
//#include "pch.h"
//#include "msg.h"
//生成的dll 是 lua_add53.dll
//luaopen_lua_add
extern "C" {
#include "E:/wLua/luaVsProject/luaVsStaticLib/src/lua.h"
#include "E:/wLua/luaVsProject/luaVsStaticLib/src/lualib.h"
#include "E:/wLua/luaVsProject/luaVsStaticLib/src/lauxlib.h"
};
#pragma comment(lib, "E:/wLua/luaVsProject/x64/Debug/luaVsDll.lib")
#include
using namespace std;
extern "C" int ShowMsg(lua_State * luaEnv) {
cout dir
2022/04/17 19:58 .
2022/04/17 19:58 ..
2022/04/17 19:58 182 luac.out
2022/04/17 19:51 182 test.exe
2022/04/17 18:50 82 test.lua
2022/04/17 19:49 66,560 test_luaDll1.dll
2022/04/17 19:49 756 test_luaDll1.exp
2022/04/17 19:49 1,900 test_luaDll1.lib
2022/04/17 19:49 1,527,808 test_luaDll1.pdb
7 个文件 1,597,470 字节
3 个目录 801,822,638,080 可用字节
E:\wLua\test_luaDll1\x64\Debug>lua luac.out
Hello world from clibs!
-1.0
从上面的运行结果可以看到,luac把test.lua变成了luac.out,这个是二进制格式的,然后通过lua.exe照样可以调用。当然,如果你希望生成的二进制文件名称有所不同,可以使用下面的指令、
luac -o myname.out test.lua、
然后,就可以这样调用了,
lua myname.out
本文结束。