首页 > 解决方案 > 错误:preLaunchTask 'C/C++:g++.exe build active file' 以退出代码 -1 终止

问题描述

我是 C++ 和 VS Code 的新手。

我正在尝试使用 g++ 在 VS Code 中调试我的 C++ 代码。

但我收到此错误消息: preLaunchTask 'C/C++:g++.exe build active file' 以退出代码 -1 终止。

这是我的代码:

/*blabla*/

这是我的tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\usr\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\msys64\\usr\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\msys64\\usr\\bin\\g++.exe"
        }
    ]
}

这是我的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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:\\msys64\\usr\\bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\usr\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

这是我的终端中的内容:

> Executing task: C/C++: g++.exe build active file <

Starting build...
Build finished with errors(s):
/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/bin/ld: /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/libmsys-2.0.a(libcmain.o): in function `main':
/c/_/S/msys2-runtime/src/msys2-runtime/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/c/_/S/msys2-runtime/src/msys2-runtime/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x86): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

The terminal process terminated with exit code: -1.

Terminal will be reused by tasks, press any key to close it.

> Executing task: C/C++: g++.exe build active file <

Starting build...
Build finished with errors(s):
/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/bin/ld: /usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../lib/libmsys-2.0.a(libcmain.o): in function `main':
/c/_/S/msys2-runtime/src/msys2-runtime/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/c/_/S/msys2-runtime/src/msys2-runtime/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x86): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

The terminal process failed to launch (exit code: -1).

Terminal will be reused by tasks, press any key to close it.

我昨天可以成功调试代码。然后我安装了 git 和 SSH(不确定这是否相关,但包括它以防万一)。

今天,我尝试调试代码,但我无法再成功。

我试图用谷歌搜索错误消息,但无法找出问题所在。

标签: c++jsondebuggingvisual-studio-codeg++

解决方案


推荐阅读