c++与lua互相调用(内嵌式)
1.下载lua官网
2.解压 tar-zxvf
3.将源码导入项目里面,去掉lua.c和luac.c里面的main方法,在使用之前创建一个lua.hpp文件,作为后面的头文件
#ifndef LUA_HPP
#define LUA_HPP
// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#endif // LUA_HPP
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
audio.cpp \
lua/lapi.c \
lua/lauxlib.c \
lua/lbaselib.c \
lua/lcode.c \
lua/lcorolib.c \
lua/lctype.c \
lua/ldblib.c \
lua/ldebug.c \
lua/ldo.c \
lua/ldump.c \
lua/lfunc.c \
lua/lgc.c \
lua/linit.c \
lua/liolib.c \
lua/llex.c \
lua/lmathlib.c \
lua/lmem.c \
lua/loadlib.c \
lua/lobject.c \
lua/lopcodes.c \
lua/loslib.c \
lua/lparser.c \
lua/lstate.c \
lua/lstring.c \
lua/lstrlib.c \
lua/ltable.c \
lua/ltablib.c \
lua/ltm.c \
lua/lua.c \
lua/luac.c \
lua/lundump.c \
lua/lutf8lib.c \
lua/lvm.c \
lua/lzio.c \
main.cpp \
mainwindow.cpp
HEADERS += \
audio.h \
include/fmod/fmod.h \
include/fmod/fmod.hpp \
include/fmod/fmod_codec.h \
include/fmod/fmod_dsp.h \
include/fmod/fmod_errors.h \
include/fmod/fmod_memoryinfo.h \
include/fmod/fmod_output.h \
include/fmod/fmodiphone.h \
include/fmod/fmodlinux.h \
include/fmod/fmodwindowsphone.h \
include/fmod/fmodwindowsstoreapp.h \
lua/lapi.h \
lua/lauxlib.h \
lua/lcode.h \
lua/lctype.h \
lua/ldebug.h \
lua/ldo.h \
lua/lfunc.h \
lua/lgc.h \
lua/ljumptab.h \
lua/llex.h \
lua/llimits.h \
lua/lmem.h \
lua/lobject.h \
lua/lopcodes.h \
lua/lopnames.h \
lua/lparser.h \
lua/lprefix.h \
lua/lstate.h \
lua/lstring.h \
lua/ltable.h \
lua/ltm.h \
lua/lua.h \
lua/lua.hpp \
lua/luaconf.h \
lua/lualib.h \
lua/lundump.h \
lua/lvm.h \
lua/lzio.h \
mainwindow.h
FORMS += \
mainwindow.ui
TRANSLATIONS += \
sgs_zh_CN.ts
LIBS += -L.
LIBS += -L"$$_PRO_FILE_PWD_/lib/"
LIBS += -lfmodex64
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
4.c++调用lua,先看界面图
界面说明,左边播放声音的按钮,右边播放bgm的,右边按钮播放的bgm从lua文件里面获取
两个槽函数与lua交互
void MainWindow::on_pushButton_clicked()
{
Audio::play("audio/system/BackGroundMusic02.mp3",false);
}
void MainWindow::on_pushButton_2_clicked()
{
// 初始化lua虚拟机
lua_State * l = luaL_newstate();
luaL_openlibs(l);
//这里用loadfaile,不执行
int result =luaL_loadfile(l, "ext/bgm.lua");
if (result){
printf("load file error,result=%d\n",result);
}
// 执行lua文件,四个参数说明 lua_State *,参数个数,返回结果,最后一个表示出错的信息,具体含义还未知
result = lua_pcall(l, 0, 0, 0);
if (result){
printf("load file error,result=%d\n",result);
}
//读取lua文件里面全局变量a,
lua_getglobal(l, "a");
//获取栈顶的数据,返回的是字符串
const char* luaa = lua_tostring(l, -1);
//转化为c++的string
string a = luaa;
cout << "a=" << a << endl;
//读取b,转化为QString
lua_getglobal(l, "b");
QString b(lua_tostring(l, -1));
cout << "b=" << b.toStdString() << endl;
// 获取并调用函数,第二个参数是函数名
lua_getglobal(l, "fun");
//把0作为置入栈顶
lua_pushnumber(l, 0);
//调用
int iRet = lua_pcall(l, 1,1, 0);
if (iRet){
const char* error = lua_tostring(l, -1);
cout << error << endl;
lua_close(l);
}
// 获取结果
if (lua_isstring(l, -1)){
QString s = lua_tostring(l, -1);
//播放bgm
Audio::playBGM(s);
}
lua_close(l);
}
lua文件
a = "E:/qt/workspace/sanguosha/sgs/audio/skill/juejing.ogg"
b = "E:/qt/workspace/sanguosha/sgs/audio/skill/jueqing1.ogg"
function fun(n)
if n == 0 then
return a
else
return b
end
end
c = "wo bu shi ni da ye haha ha"
-- 这个用来调用c++的
helloLua(c)
5.lua调用c++
#include "mainwindow.h"
#include <QApplication>
#include "lua/lua.hpp"
#include<iostream>
using namespace std;
#include<QDebug>
extern "C" int helloLua(lua_State *l){
//取出参数
printf("%s\n",lua_tostring(l, -1));
return 1;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 初始化lua虚拟机
lua_State * l = luaL_newstate();
qDebug()<< "逗比啊"<<endl;
cout<<"哈哈,出bugle"<<endl;
if(l==NULL){
printf("哈哈,出bugle");
}
luaL_openlibs(l);
const char *buf = "print('wo qu ni da ye de lua!')";
//注册函数
lua_register(l, "helloLua", helloLua);
luaL_dofile(l, "ext/bgm.lua");
luaL_dostring(l,buf);
//销毁指定虚拟机的所有对像
lua_close(l);
MainWindow w;
w.show();
return a.exec();
}
调用大致思路,把数据放入栈,另一个语言去取数据,大致原理就这样子
磨刀不误砍柴工,先稍微巩固一下基础
太阳神三国杀第一弹,lua与c++交互,下一期把fmodex代码搬上来。