windows平台使用vscode搭建Lua与C++交互环境(附C++环境搭建json文件)

一.下载Lua源码

注意:如果下载的只是DLL拓展是不能进行Lua与C++交互的,像这种:

必须下载源码。

请下载Lua源码:https://www.lua.org/ftp/lua-5.4.3.tar.gz

二.编译

到lua-xxx/src下进行编译(请自行下载MingW编译器):

mingw32-make mingw

三.vscode下的设置

1.c_cpp_properties.json文件中设置路径:

"includePath": [
                "${workspaceFolder}",
                "D:/lua-5.4.4/src/**"
            ],

2.ctrl+shift+P找到Edit Configurations设置包含路径

 找到包含路径选项并设置:

 3.task.json文件里设置args(这是最主要的一步,上面两步只是让我们写头文件的时候可以不用写绝对路径而已)
 

"args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-L",
                "D:/lua-5.4.4/src",
                "-I",
                "D:/lua-5.4.4/src",
                "-llua",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],

-I,-L,-l参数大家自行学习

四:简单的一个交互使用

extern "C"//如果是C++文件请这样写
{
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    #include "lua.hpp"
}
#include <string.h>

char const* scripe = R"(
function Print()
    print('hello world!')
end
    Print();
)";

int main()
{
    lua_State *state = luaL_newstate();//初始化lua虚拟机
    luaL_openlibs(state);{
        luaL_loadbuffer(state,scripe,strlen(scripe),"Print");
        lua_pcall(state,0,0,0);
    }
    lua_close(state);
    return 0;
}

五:vscode下C/C++环境配置(直接拿来就可以用)

1.c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

2.launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

3.settings.json

{
    "files.associations": {
        "ostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp",
        "thread": "cpp",
        "chrono": "cpp"
    }
}

4.tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

六:常见的问题

1.unrecognized command line option '-i'

vscode下task不支持-i参数指定头文件,不过不用管,不影响

2.C++文件没有extern "C"

3.unreference xxx:未指定库

"-llua"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值