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

macOS下如何在VS Code集成终端交互式运行C++代码

解决macOS下VS Code中clang运行带输入C代码的交互问题

正确配置launch.json

替换或修改你的.vscode/launch.json为以下内容,重点确保控制台指向集成终端,且预启动任务与编译任务匹配:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file",
            "console": "integratedTerminal"
        }
    ]
}

正确配置tasks.json

修改.vscode/tasks.json,确保编译命令正确,且终端设置为自动显示:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: /usr/bin/clang++",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            }
        }
    ]
}

额外VS Code设置调整

打开VS Code设置(快捷键Cmd+,),搜索并修改以下选项:

  • 关闭Debug Console自动打开:搜索debug.openDebug,设置为neverOpen
  • 确保默认终端配置正确:搜索terminal.integrated.defaultProfile.osx,选择zsh或bash(根据你的默认shell)

验证步骤

  1. 保存上述两个配置文件
  2. 打开你的C++代码文件
  3. 点击VS Code左侧调试面板的绿色运行按钮,或按F5启动调试
  4. 此时集成终端会自动弹出,你可以直接在终端中输入cin所需的内容,cout输出也会显示在终端里

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 06:23:14