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

WSL2中VSCode的CMake Tools忽略正确路径下的launch.json

WSL2下VSCode CMake Tools调试时launch.json配置不生效问题

问题概述

在WSL2环境中使用VSCode结合CMake Tools构建并调试C项目,手动创建launch.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.1",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/cipherffff",
            "args": ["a"],
            "environment": [
                {
                    "name": "config",
                    "value": "Debug"
                }
            ],
            "cwd": "${workspaceFolder}"
        }
    ]
}

重点关注program和args字段

确认VSCode已读取launch.json

通过inotifywait命令验证文件被成功读取:

$ inotifywait -e open -m .vscode/launch.json
Setting up watches.
Watches established.
.vscode/launch.json OPEN
.vscode/launch.json OPEN
.vscode/launch.json OPEN

调试日志显示配置未被应用

日志中实际加载的是CMake Tools自动生成的配置,与自定义配置不符:

[main] Debug configuration from cache: {"type":"cppdbg","name":"Debug cipher","request":"launch","cwd":"/home/MYUSER/project_name/build","args":[],"MIMode":"gdb","miDebuggerPath":"/usr/bin/gdb","setupCommands":[{"description":"Enable pretty-printing for gdb","text":"-enable-pretty-printing","ignoreFailures":true}],"program":"/home/MYUSER/project_name/build/cipher"}
[main] Starting debugger with following configuration. {"workspace":"file:///home/MYUSER/project_name","config":{"type":"cppdbg","name":"Debug cipher","request":"launch","cwd":"/home/MYUSER/project_name/build","args":[],"MIMode":"gdb","miDebuggerPath":"/usr/bin/gdb","setupCommands":[{"description":"Enable pretty-printing for gdb",....

调试GUI中也未显示自定义运行参数,当前使用的是CMake Tools调试器而非默认调试器。

环境版本

  • CMake Tools v1.19.52
  • VSCode 1.94.2
  • Ubuntu 24.04.1 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

解决方案

1. 关联CMake Tools与自定义launch配置

在.vscode/settings.json中添加配置,指定CMake Tools使用自定义的launch配置:

{
    "cmake.debugLaunchConfiguration": "(gdb) Launch"
}

此配置会让CMake Tools调试时直接调用你定义的(gdb) Launch配置。

2. 禁用CMake Tools自动生成调试配置

若希望完全使用手动配置,可在settings.json中关闭自动生成:

{
    "cmake.automaticDebugConfig": false
}

3. 直接在CMake Tools中指定调试参数

如果不想依赖launch.json,也可直接在settings.json中配置调试参数:

{
    "cmake.debugArgs": ["a"],
    "cmake.debugConfig": "${workspaceFolder}/build/cipherffff",
    "cmake.debugWorkingDirectory": "${workspaceFolder}"
}

4. 验证可执行文件路径

确认${workspaceFolder}/build/cipherffff路径正确,CMake实际生成的可执行文件名称与路径匹配,避免因文件不存在导致配置被忽略。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 20:11:03