错误:"preLaunchTask 'C/C++:g++.exe构建活动文件'以退出码1终止"

6

enter image description here

我读到这个错误是因为launch.json和tasks.json文件中的一些设置。所以我删除了它们并创建了新的文件,但它仍然显示相同的错误,并且无法构建和调试。我该怎么办?

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": "${workspaceFolder}",
            "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++: g++.exe build active file"
        }
    ]
}

Tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}
9个回答

5
在我的情况下,
  1. 删除自动生成的.vscode文件夹,该文件夹在第一次调试时会自动生成。
  2. 现在再次进行调试(按F5或右上角的Debug C/C++ File按钮)。

如果编译器有任何列表,请选择第一个。

99%的情况下这将起作用。 否则,请尝试带有调试选项的在线C++编译器,例如

https://www.onlinegdb.com/online_c++_compiler


1
在我的情况下,这个方法有效。 - ph0en1x
1
在我的情况下,这个方法有效。 - undefined
工作中,谢谢。 - undefined

4

默认情况下,Vs. code一次只能编译一个C++文件。因此,我们需要告诉编译器编译该项目文件夹中的所有.cpp文件。您的"tasks.json"文件应为:

{
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "-Wall", <====== to give all warning
                "-std=c++17", <======== specific version of the complier
                "${fileDirname}/*.cpp",<======= Compile all CPP files
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

1
如果您之前已经编译了代码并且可执行文件(*.exe)已经创建,请通过VS代码文件浏览器将其删除,然后尝试重新构建代码。 这就是问题所在,至少在我的情况下。 VS代码无法删除可执行文件以创建新文件。

0

在我的情况下,问题出在 gcc.exe 文件的位置与 tasks.json 文件中命令位置不匹配,只需更改此设置即可解决问题。

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Users\\hrish\\Documents\\gcc\\bin\\gcc.exe",/* here the file location was different */
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

0
我最近遇到了这个问题,解决方法是选择g++作为编译器,而不是gcc。

This is the image

gcc和g++之间的区别在于,gcc是为C程序设计的,但也可以运行C++程序,而g++则专门用于运行C++程序。
要访问下拉菜单以切换编译器,您需要删除.vscode文件夹,然后运行您的C++文件。

0

在我的情况下:

  • 我删除了 .vscode 文件夹中的内容,
  • 关闭了远程连接,
  • 关闭并重新打开了VSCode,
  • 点击右上角的 运行c/c++文件 按钮,程序成功执行。

0

只需关闭屏幕下方的VS代码终端选项卡中打开的所有终端。(如果有多个终端同时运行,可能无法删除旧的exe文件以创建新的。) 查看此照片以获得清晰度


1
根据当前的表述,你的回答不明确。请[编辑]以添加额外的细节,帮助他人理解这个问题的解答方式。你可以在帮助中心找到关于如何撰写好的回答的更多信息。 - Shawn

0
我曾经遇到过这个问题。如果“main()”名称有误,就会出现这种情况。例如:
如果你写成int mai()而不是main(),就可能导致这样的问题。如果你提供你的代码,我们可以进一步评估可能存在的问题。
如需额外帮助:
你可以将你的launch.json文件修改为以下内容:
{ 
    //Use IntelliSense to understand related attributes. 
    //Hover to see descriptions of existing properties. 
    //For more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387 
    "version" :  "0.2.0" , 
    "configurations" :  [ 
        { 
            "name" :  "g++. exe-Generate and debug activity files" , 
            "type" :  "cppdbg" , 
            "request" :  "launch" , 
            "program" :  "${fileDirname}\\${fileBasenameNoExtension}.exe" , 
            "args" :  [ ] , 
            "stopAtEntry" :  false ,
             
            "environment" :  [ ] , 
            "externalConsole" :  false , 
            "MIMode" :  "gdb" , 
            "miDebuggerPath" :  "F:\\VScode\\mingw64\\bin\\gdb.exe" , 
            "setupCommands" :  [ 
                { 
                    "description" :  "Enable neat printing for gdb" , 
                    "text" :  "-enable-pretty-printing" , 
                    "ignoreFailures" :  true 
                } 
            ] , 
            "preLaunchTask" :  "C/C++: g++.exe build active file"
        } 
    ] 
}

将miDebugger更改为MinGW路径,如果问题仍然存在,则可以尝试将externalConsole更改为true


很好,但是VSCode报错说缺少当前工作目录路径。 - WARUTS

-1
你可以删除mingw目录并安装新的。打开这个链接https://sourceforge.net/projects/mingw-w64/files/mingw-w64/download the One with dwarf at its end

打开这个链接。在我的情况下,这是最新的版本,但在你的情况下可能是相同的或不同的。使用WinRAR来提取文件。mingw32现在设置了环境变量。VS Code现在应该可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接