当前位置:网站首页>Lua导出为外部链接库并使用
Lua导出为外部链接库并使用
2022-06-21 16:09:00 【ufgnix0802】
Lua导出为外部链接库并使用
TIPS:以下示例使用Lua-5.3.6版本进行介绍,结合VS2022进行演示。
下载安装


压缩包内容如下:

紧接着,使用VS2022创建一个Windows桌面向导,将Lua源码压缩包内部src文件的源代码文件拷贝至VS2022创建的项目中,具体步骤如下:



将Lua源文件中str文件夹下除Makefile和luac.c文件外所有文件复制进入Project1项目路径下。
关于为何去除luac.c文件,可参考:https://blog.csdn.net/qq135595696/article/details/124563336
src文件夹:
Project1项目文件:

然后进入VS2022中,添加现有项(将所有.h和.c文件都加入)。

如下:
luaconf.h
打开luaconf.h文件,找到第244行,如下

赋值上述红框中的宏,添加至项目–>属性–>C/C+±->预处理器–>预处理器定义中。然后点击应用即可。



结果如下:

接下来,配置一下项目的输出目录和中间目录,配置步骤请参考如下链接(路径相同):https://blog.csdn.net/qq135595696/article/details/121366990
最后,右键项目生成,即可导出相应的dll库和lib库。


Lua外部链接库的应用
在原有解决方案的基础上,重新添加一个项目,如下:

新建一个C++空项目,如下:



同时配置一下输出目录和中间目录,同上。
导入文件并执行
将如下文件从Project1项目导入Project2项目中,如下:



接下来将上述文件在Project2项目中添加现有项。

紧接着,新建一个main.cpp执行文件,同时代码如下:
TIPS:关于以下C/C++与Lua的交互可以参考博主的Lua模块部分内容,这里不进行细述。
#include <stdio.h>
#include <stdlib.h>
#include "lua.hpp"
#pragma comment(lib,"Project1.lib")//引入lib库文件,需要配置附加库目录,下文进行简述。
extern "C" {
// 一个Lua函数的标准模型
LUALIB_API int lua_TestFunc(lua_State* L)
{
int top = lua_gettop(L);
const char* str = luaL_checkstring(L, 1);
int num = luaL_checkinteger(L, 2);
top = lua_gettop(L);
printf("lua调用C函数 %d %s %d\n", top, str, num);
lua_pushstring(L, "Hello World");
lua_pushnumber(L, 123);
// 表示有2个返回值
return 2;
}
}
int main(int argc, char** argv) {
lua_State* L = luaL_newstate(); /* create state */
luaL_openlibs(L);
// 注册函数
// 缺点:注册的函数为全局函数
lua_register(L, "testFunc", lua_TestFunc);
// 运行一个lua文件或者说加载一个lua文件
int nRet = luaL_dofile(L, "test.lua");
luaL_dostring(L, "print('Hello Lua string')");
luaL_dostring(L, "print(10+20*30)");
printf("===%d===\n", lua_gettop(L));
lua_getglobal(L, "gg");
int num = lua_tonumber(L, 1);
printf("%d===\n", num);
printf("===%d===\n", lua_gettop(L));
lua_pop(L, 1);
printf("===%d===\n", lua_gettop(L));
// 执行完成后关闭lua虚拟机
lua_close(L);
return EXIT_SUCCESS;
}
test.lua
--test.lua
print("Hello Lua")
function func()
-- return 1, 2, 3, 4
local a, d = testFunc("string", 333)
print(a, d)
end
-- local a,_,_,d = func()
-- print(a,d)
-- local a, d = testFunc("string", 333)
-- local a, d = testFunc()
-- print(a, d)
-- print(type(a),type(d))
func()
gg = 33333
最后,由于我们生成的lib文件位于$(SolutionDir)…/bin/$(Platform)$(Configuration)路径下(即为输出目录,与上文配置的输出目录路径相同),我们需要对Project2–>右键属性–>链接器–>常规–>附加库目录进行配置,如下:

点击应用即可。
注意点
务必更改启动项目,将Project2设置为启动项目,否则会出现如下情况。

运行结果

边栏推荐
- Lagrange interpolation
- 堆栈认知——栈溢出实例(ret2text)
- 直播平台开发,直播各个分类单例设计展示
- PTA l3-032 questions about depth first search and reverse order pair should not be difficult (30 points)
- Kotlin常用函数 let,with,apply,also,run
- node服务器 res.end()中写中文,客户端中乱码问题的解决方法
- 【技术管理】集结号与亮剑团队
- 《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别
- Create a server with node
- 加密大崩盘,Web3游戏到底还有没有未来?5篇论文深度探讨
猜你喜欢

Yrcloudfile of Yanrong technology has completed the compatibility certification with ANTP to jointly create a new blueprint for storage

Lagrange interpolation

拦截器实现网页用户登陆

【蓝桥杯省赛真题35】Scratch水面倒影 少儿编程scratch编程蓝桥杯省赛真题讲解

Jetpack Compose 管理状态(一)

Common setting modes

软件测试体系学习及构建(13)-测试基础之测试工程师的基本要求

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

为什么RedisCluster设计成16384个槽?

Compose programming idea
随机推荐
regular expression
潤邁德醫療通過上市聆訊:預計虧損將增加,霍雲飛兄弟持股約33%
正则表达式
多维分析预汇总应该怎样做才管用?
Kotlin DSL构建
室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?
National administrative division
第五章 操作位和位串
3de 3D model View ne voit pas comment ajuster
Why is rediscluster designed with 16384 slots?
Algorithm -- maximum number after parity exchange (kotlin)
In the "roll out" era of Chinese games, how can small and medium-sized manufacturers solve the problem of going to sea?
3DE 三维模型视图看不到怎么调整
Class, interface, function
Iframe cross domain value transfer
Google play 应用签名密钥证书,上传签名证书区别
iframe跨域传值
wcdma与LTE的区别
《MATLAB 神经网络43个案例分析》:第26章 LVQ神经网络的分类——乳腺肿瘤诊断
火山引擎+焱融 YRCloudFile,驱动数据存储新增长