首页 > 解决方案 > 在 C 中嵌入 Lua 5.1,无法调试,VSCode 中的预启动任务错误

问题描述

我正在尝试了解如何将 Lua 5.1 嵌入到 C 文件中,以创建一个最小可重现示例来尝试解决另一个问题(此处的线程)。我正在关注这个简单的教程,但是一旦调试器到达lua_State *L = luaL_newstate();,它就会给我一个错误,即预启动任务 'C/C++:gcc.exe build active file' 以退出代码 -1 终止

如何修复此错误?我尝试搜索答案,但找不到与这种情况相匹配的任何内容,我不知道在 C++ 中嵌入 Lua 是否与 C 不同。我在 Windows 10 上使用 VSCode。

这是我在 C 文件中尝试的代码:

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int main(void) {
  lua_State *L = luaL_newstate();
  return 0;
}

这是我的launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

这是我的tasks.json:

 {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: gcc.exe build active file",
                "command": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe",
                    "-IC:\\Program Files\\MyLibs\\lua5.1\\include"
                ],
                "options": {
                    "cwd": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }

标签: cvisual-studio-codegccluaembedding

解决方案


问题原来是缺少链接器信息。很难找到特定于在 Windows 上使用 GCC 和 VSCode 将 Lua 嵌入 C 的信息,有一段时间我不确定这些命令应该去哪里。由调试器自动创建的 tasks.json 不包含此信息。我必须在该部分的括号内输入"-LC:/MyLibs/lua5.1","-IC:/MyLibs/lua5.1/include",和。这就是文件现在的样子(自从第一次发布以来,我已经将 minGW 和 MyLibs 移到“程序文件”之外,以查看文件夹名称中的空格是否使 GCC/GDB 起作用):"-llua5.1","args"

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-LC:/MyLibs/lua5.1",
                "-IC:/MyLibs/lua5.1/include",
                "-llua5.1",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

推荐阅读