在 C 中嵌入 Lua 5.1,无法调试,VSCode 中的预启动任务错误

Embedding Lua 5.1 in C, Can't Debug, Prelaunch Task Error in VSCode

我正在尝试了解如何将 Lua 5.1 嵌入到 C 文件中,以便创建一个最小的可重现示例来尝试解决另一个问题(thread here). I was following this simple tutorial 但一旦调试器到达 lua_State *L = luaL_newstate();,它给我错误 预启动任务 'C/C++:gcc.exe build active file' 以退出代码 -1 终止。

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

这是我在 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"
    }

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

{
    "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"
}