【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】

————————– 2018.03.27 更新————————-
便携版已更新,点此获取便携版
已知BUG:中文目录无法正常调试
用于cpptools 0.15.0插件的配置文件更新
新的launch.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files (x86)/MinGW/bin/gdb.exe", // GDB的路径,注意替换成自己的路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

新的tasks.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process


{
    "version": "2.0.0",
    "tas
### 配置 VSCode 中 C 和 C++ 编译调试任务 #### 创建 `tasks.json` 文件 为了配置构建任务,在 VSCode 中需通过命令面板创建 `tasks.json` 文件。具体操作为按下 Ctrl + Shift + P (Windows/Linux) 或 Command + Shift + P (macOS),随后输入 “Tasks: Configure Default Build Task”,并选择合适的编译器,例如对于 Windows 系统下的 GCC 编译器(MinGW-w64),应选择 “C/C++: g++.exe build active file”[^2]。 此过程会在 `.vscode` 文件夹内自动生成一个名为 `tasks.json` 的文件,其内容定义了如何调用外部命令(如 gcc 或者 g++)来执行编译工作,包括设定命令行参数以及指定的工作路径等信息[^1]。 下面是一个适用于 Linux 平台上的简单 C 语言项目的 `tasks.json` 示例: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "/usr/bin/gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task to compile a single C file using gcc" } ] } ``` 这段 JSON 定义了一个标签为 `"build hello world"` 的 shell 类型任务,它会利用 `/usr/bin/gcc` 命令加上 `-g` 参数来进行带有调试信息的编译,并将输出目标文件保存到当前源码所在的同一目录下,同时指定了错误匹配模式 `$gcc` 来帮助识别编译过程中可能出现的问题。 对于 macOS 用户来说,如果采用 clang++ 作为编译工具,则可以参照类似的结构调整相应的字段值,比如更改 command 字段指向系统的 clang++ 可执行文件位置[^3]。 #### 关联 `launch.json` 进行调试准备 当完成了上述针对 C/C++ 源代码的编译配置之后,还需要进一步设置 `launch.json` 文件以便能够顺利启动调试流程。这两个文件共同协作确保开发人员可以在集成环境中高效地完成从编写代码直至最终排查问题的一系列操作。
评论 116
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值