lua是很不错的脚本语言,它很好的实现了c与lua交互,这里我记录一下lua和C交互的基础操作步骤;
一、下载lua源码,这里我们需交互C语言,所有我不在讲述Lua环境搭建,如有需要见我另外帖子https://blog.csdn.net/qq_15725099/article/details/81867483
Lua官网:http://www.lua.org/,下载最新发布版本源码
下载源码方法二:
二、编译前准备
vs新建项目,选择win32控制台应用
确定后下一步,选择DLL和空项目,点击完成
解压lua源码
复制src整个文件夹到,新建项目路径下,如图
删除该文件夹中的lua.c和luac.c两个文件,这两个文件不编译
将所有文件添加到项目工程中
三、修改编译属性
四、直接编译即可,编译后会生成.lib和dll库,.lib用于开发链接使用,dll用于运行使用,也可直接编译静态库使用,这里不再讲述,作者编译后如图
五、抓出库需要的include文件
注:除lua.hpp都是C风格头文件,如果要在c++中使用记住加上extern "C",或者直接使用lua.hpp。其实lua.hpp就是加了extern "C"。
至此,库编译就结束了,以下是我使用的一个项目部分源码,开发环境是QT+VS,主要实现Lua调C和C调lua
#include "luaparsescriptclass.hpp"
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QApplication>
#include <QMetaType>
// 定义全局打印数据
static void printToTextEdit(QString strValue)
{
if (m_globalPrintTextEdit == nullptr)
{
return;
}
// 获取系统时间
QString strDataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss:zzz");
m_globalPrintTextEdit->setTextColor(QColor(255, 255, 255));
m_globalPrintTextEdit->append("[" + strDataTime + "]:");
m_globalPrintTextEdit->setTextColor(QColor(189, 99, 197));
m_globalPrintTextEdit->append(strValue);
}
// 定义lua调用c++函数打印lua日志
static int printLuaLog(lua_State* L)
{
// 检测参数个数
int iValueCount = lua_gettop(L);
if (iValueCount != 1)
{
printToTextEdit("Lua parameter error" + QString::number(iValueCount));
return 0;
}
// 判断数据类型重新打印到界面
int iValueType = lua_type(L, 1);
QString strPrintValue = "";
switch (iValueType)
{
case LUA_TBOOLEAN:
{
strPrintValue = lua_toboolean(L, 1) ? "true " : "false ";
}
break;
case LUA_TNUMBER:
{
strPrintValue = QString::number(lua_tonumber(L, 1));
}
break;
case LUA_TSTRING:
{
strPrintValue = lua_tostring(L, 1);
}
break;
default:
{
strPrintValue = lua_typename(L, 1);
printToTextEdit("Lua type error");
}
break;
}
printToTextEdit(strPrintValue);
return 1;
}
// 初始化lua运行
void LuaParseScriptClass::initLuaExecute()
{
// 开启运行服务脚本线程
m_luaServerScript = new luaServerScriptClass(m_ServertextEdit);
// 信号和槽连接
connect(m_luaServerScript, SIGNAL(signal_returnExecResult(QByteArray)), this, SLOT(slot_recvServerData(QByteArray)));
m_luaServerScript->start();
// 创建lua运行state
m_globalLState = luaL_newstate();
luaL_openlibs(m_globalLState);
//为Lua注册名为第一个参数的函数,实际上是调用c++中第三个参数名的函数
lua_register(m_globalLState, "printLog", printLuaLog);
//读lua文件并运行Lua code
// 打开js文件
QString strJsPath = QApplication::applicationDirPath() + "/LuaScript/LuaParseScript.lua";
int fret = luaL_dofile(m_globalLState, strJsPath.toStdString().c_str());
if (fret)
{
printTextEdit("read lua file error!" + strJsPath);
}
}
// 读取数据
void LuaParseScriptClass::parseData(QByteArray arrayData)
{
if (m_globalLState == nullptr)
{
printTextEdit("parse data error!");
}
//取函数
lua_getglobal(m_globalLState, "add");
lua_pushnumber(m_globalLState, 15);
lua_pcall(m_globalLState, 1, 1, 0);//2-参数格式,1-返回值个数,调用函数,函数执行完,会将返回值压入栈中
printTextEdit("parse data value:" + QString(lua_tostring(m_globalLState, -1)));
if (m_luaServerScript)
{
QByteArray arraydata;
arraydata.append(QString("hello Lua").toLatin1());
m_luaServerScript->executeLuaServer(arraydata);
}
}
function add(a)
prinInfo(a)
return a
end
local numberValue = 15
function prinInfo(value)
local i = 10
while (i > 0)
do
printLog('server data' .. tostring(i))
i = i - 1
end
end
执行效果如图
如有疑问加群:676977101