yzhkpli 发表于 2025-4-1 20:39:17

win10下vscode调试c语言的环境配置


此文不涉及配置51单片机编译.仅仅用于学习c编程使用。能够debug,但是如果仅仅执行时候有问题。详见最后

win10 64位,mingw64,vscode(VSCodeSetup-x64-1.98.2)

MinGW: 支持 Windows 平台的 GCC 编译环境
https://www.mingw-w64.org/
https://github.com/niXman/mingw-builds-binaries/releases下载:x86_64-14.2.0-release-win32-seh-ucrt-rt_v12-rev2.7z
解压,将 mingw64移动到c盘根目录下


源代码目录 D:\c-blackhorse
该文件夹下面新建一个 1.c文件。右下角自动提示安装c\c++扩展。安装之。

参考文档


【C语言】概述与开发环境搭建(VSCode+MinGW)- 1.【C语言】概述与开发环境搭建(VSCode+MinGW)(Av114138235799752,P1)



以及黑马的
C++入门-08_【扩展】VSCode环境配置
在 D:\c-blackhorse 新建一个 .vscode文件夹。下面建立三个文件c_cpp_properties.json{
    "configurations": [
      {
            "name": "win32",
            "includePath": ["${workspaceFolder}/**"],
            "defines": ["_DEBUG", "UNICODE", "UNICODE"],
            "windowsSdkVersion": "10.0.17763.0",
            "compilerPath": "c:\\mingw64\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样*/
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}"
      }
    ],
    "version": 4
}

launch.json文件{
    "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": true,
            "MIMode": "gdb",
            "miDebuggerPath": "c:\\mingw64\\bin\\gdb.exe",/*修改成自己bin目录下的gdb.exe*/
            "setupCommands": [
                {
                  "description": "为 gdb 启用整齐打印",
                  "text": "-enable-pretty-printing",
                  "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task g++"
      }
    ]
}

tasks.json文件
{    "version": "2.0.0",    "tasks": [      {            "type": "shell",            "label": "task g++",            "command": "c:\\mingw64\\bin\\g++.exe",            "args": [                "-g",                "${file}",                "-o",                "${fileDirname}\\${fileBasenameNoExtension}.exe",                "-I",                "D:\\c-blackhore",                "-std=c++11"            ],            "options": {                "cwd": "c:\\mingw64\\bin"            },            "problemMatcher": [                "$gcc"            ],            "group": "build"      }}

先将c源代码文件置于activate状态,就是鼠标点中这个文件,把源码显示出来即可。
使用F5可以debug,目前已经可以debug。通过左侧的watch,可以把指定的变量值也显示出来。

不过如果仅仅运行,我还没有调试出来。执行窗口一闪而过,在    return 0;前面加上system("pause");反而报错!!!
页: [1]
查看完整版本: win10下vscode调试c语言的环境配置