json 错误:“预启动任务”C/C++:g ++. exe构建活动文件“以退出代码1终止”

ghg1uchk  于 2023-01-22  发布在  C/C++
关注(0)|答案(5)|浏览(656)

我读到这个错误是因为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"
}
ifmq2ha2

ifmq2ha21#

默认情况下,Vs.code一次只能编译一个C++文件,所以我们需要告诉编译器编译该项目文件夹中的所有.cpp文件。

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

plupiseo2#

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

xienkqul

xienkqul3#

就我而言,
1.删除第一次调试时自动生成的.vscode文件夹
1.现在再次调试(按F5或右上角Debug C/C++ File
如果编译器收到任何列表,则选择第一个。
99%这将工作。否则尝试在线C++编译器与调试选项一样
https://www.onlinegdb.com/online_c++_compiler

cs7cruho

cs7cruho4#

我曾经遇到过这样的问题。如果“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

vngu2lb8

vngu2lb85#

在我的情况下,这是一个问题的位置gcciderexe文件在命令位置的tasksiderjson文件只需改变这一点,它应该工作正常。

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

相关问题