You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code运行C++报错:preLaunchTask终止码-1/1,权限被拒

解决VS Code中C++编译权限拒绝及运行闪退问题

核心问题分析

你的错误日志明确显示cannot open output file D:\Lea\lol.exe: Permission denied,这是编译失败的直接原因;同时生成的exe运行后立即关闭是独立的控制台窗口行为问题。

分步解决方案

1. 释放被占用的exe进程

  • 打开任务管理器(快捷键Ctrl+Shift+Esc)
  • 在“进程”标签页中找到lol.exe,右键选择“结束任务”
  • 重新尝试编译调试

2. 修复文件夹权限问题

  • 右键点击D:\Lea文件夹,选择“属性”→“安全”→“编辑”
  • 选中当前登录用户,勾选“完全控制”权限,点击“确定”保存
  • 或者直接将代码文件移动到用户目录(如C:\Users\你的用户名\Documents\C++),这类目录默认拥有完整读写权限

3. 修改VS Code任务配置,自动清理旧exe

编辑.vscode/tasks.json,添加前置任务自动删除旧exe,避免进程占用冲突:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "delete-old-exe",
            "type": "shell",
            "command": "del",
            "args": ["${fileDirname}\\${fileBasenameNoExtension}.exe"],
            "problemMatcher": []
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": ["$gcc"],
            "group": {"kind": "build", "isDefault": true},
            "detail": "编译器: C:\\msys64\\ucrt64\\bin\\g++.exe",
            "dependsOn": ["delete-old-exe"]
        }
    ]
}

4. 解决exe运行后立即关闭问题

有两种可选方案:

方案A:修改代码添加暂停

在代码末尾添加暂停语句,需要包含<cstdlib>头文件:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
    cout << "Hello World!";
    system("pause"); // 暂停等待用户输入
    return 0;
}

方案B:配置VS Code调试器使用外部控制台

生成并编辑.vscode/launch.json,开启外部控制台:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "启用gdb整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

5. 排查杀毒软件拦截

部分杀毒软件会误报编译生成的exe文件,导致权限拒绝:

  • 临时关闭杀毒软件,重新编译测试
  • 若问题解决,将项目文件夹添加到杀毒软件的信任列表中

内容的提问来源于stack exchange,提问作者abacba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 09:10:09