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

Time:10-06

I'm trying to see how to embed Lua 5.1 into a C file in order to create a minimum reproducible example to try and solve another problem (thread here). I was following this simple tutorial but as soon as the debugger gets to lua_State *L = luaL_newstate();, it gives me the error the prelaunch task 'C/C :gcc.exe build active file' terminated with exit code -1.

How do I fix this error? I tried searching for answers but couldn't find anything that matched this situation and I don't know if embedding Lua in C is different from C. I'm using VSCode on Windows 10.

This is the code I tried in my C file:

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

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

This is my 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"
        }
    ]
}

This is my 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"
    }

CodePudding user response:

The problem turned out to be lack of linker information. It was hard to find info specific to using GCC with VSCode on Windows to embed Lua in C, and for a while I wasn't sure where those commands were supposed to go. Having tasks.json automatically created by the debugger didn't include this info. I had to type in "-LC:/MyLibs/lua5.1",, "-IC:/MyLibs/lua5.1/include",, and "-llua5.1", within the brackets of the "args" section. This is what the file looks like now (I've moved minGW and MyLibs outside of "Program Files" since first posting to see if the spaces in the folder name were making GCC/GDB act up):

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