windows下,vscode配置luabridge3笔记
安装MinGW-w64
下载地址:sourceforge.net/projects/mi…
假定安装后,路径为D:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\
则需要把 D:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin 加入环境变量中
下载lua-5.4.2源码
下载地址 www.lua.org/ftp/
下载LuaBridge3源码
vscode 配置
1. 把解压后的 lua-5.4.2文件夹拖入 vscode
2. 在 .vscode 文件夹下创建文件 tasks.json。其内容如下
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "mingw32-make",
"args": [
"mingw" // 定义平台。
],
"group": "build",
"problemMatcher": [
"$gcc"
]
}
]
}
成功运行后, 将生成lua.exe, luac.exe, lua54.dll
3. 使用LuaBridge3
3.1 在C++中调用LUA脚本
新建一个文件夹,如 luabrige_demo。在文件夹中,新建文件,比如test.cpp。
在文件夹下,放入头文件 lua.h、lualib.h、lauxlib.h、LuaBridge.h、luaconf.h
在文件夹下,放入dll库 lua54.dll
其中,test.cpp的内容如下
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
} // extern "C"
#include "LuaBridge.h"
#include <iostream>
void SayHello() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace(L)
.addFunction("sayHello", SayHello);
luaL_dostring(L, "sayHello()");
lua_close(L);
system("pause");
}
把test.cpp打开,然后依次点击 Terminal->Run Task... -> + Configure A Task -> C/C++: g++.exe生成活动文件
修改tasks.json文件
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "test",
"command": "g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17", //指定C++17标准
"-L${fileDirname}", // -L代码连接的库的位置
"-llua54" // -l代表库的名字
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"detail": "编译器: D:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"
}
]
}
把当前窗口切换到 test.cpp,执行Task:依次点击 Terminal -> Run task -> test。
成功执行后,会生成test.exe文件
3.2 在LUA中调用C++库
编写cpp文件,如取名hello.cpp
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
} // extern "C"
#include "LuaBridge.h"
#include <iostream>
void SayHello() {
std::cout << "Hello, World!" << std::endl;
}
extern "C" {
LUALIB_API int luaopen_hello(lua_State* L){
// int parentIndex = lua_gettop(L);
luabridge::getGlobalNamespace(L)
.addFunction("sayHello", SayHello);
return 1;
}
}
添加tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "hello",
"command": "g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.dll",
"-std=c++17", //指定C++17标准
"-fPIC",
"-shared",
"-L${fileDirname}", // -L代码连接的库的位置
"-llua54" // -l代表库的名字
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"detail": "编译器: D:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"
}
]
}
生成的dll文件。通过require后,可以直接访问函数SayHello。
命名空间
上面3.2的hello.cpp例子中,把函数注册到了LUA的 _G全局变量中,如果要通过库名.函数名的形式访问,则应该在addFunction之前,创建Namespace。修改后的代码如下
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
} // extern "C"
#include "LuaBridge.h"
#include <iostream>
void SayHello() {
std::cout << "Hello, World!" << std::endl;
}
extern "C" {
LUALIB_API int luaopen_hello(lua_State* L){
// int parentIndex = lua_gettop(L);
luabridge::getGlobalNamespace(L)
.beginNamespace("hello")
.addFunction("sayHello", SayHello)
.endNamespace();
return 1;
}
}
4. 使用make编译lua源码
tasks.json文件内容如下
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "mingw32-make",
"args": [
"mingw"
],
"group": "build",
"problemMatcher": [
"$gcc"
]
},
]
}