vscode c/c++ Windows配置(mingw64)

本文详细介绍了如何在VSCode中配置C/C++环境,包括Launch.json、tasks.json和c_cpp_properties.json三个核心文件的具体设置,适用于使用mingw64的Windows开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要参考:https://blog.youkuaiyun.com/qq_40477151/article/details/103917156
具体文件配置如下

c语言三文件配置

Launch.json

{  
    "version": "0.2.0",  
 "configurations": [  
        {  
         "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
         "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
         "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
         "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
         "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "gcc", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
         "setupCommands": [  
                {   
		    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

tasks.json

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558 
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
      {
          "type": "shell",
          "label": "gcc", //注意修改
          "command": "C:\\MinGW\\bin\\gcc.exe",
          "args": [
              "-g",
              "${file}",
              "-o",
              "${fileDirname}\\${fileBasenameNoExtension}.exe",
              "-ggdb3",   // 生成和调试有关的信息
              "-Wall",    // 开启额外警告
              "-static-libgcc",   // 静态链接
              "-std=c11",       // 使用C11标准
              "-finput-charset=UTF-8",    //输入编译器文本编码 默认为UTF-8
              "-fexec-charset=GB18030",   //输出exe文件的编码
              "-D _USE_MATH_DEFINES"
          ],
          "options": {
              "cwd": "C:\\MinGW\\bin"
          },
          "problemMatcher": [
              "$gcc"
          ],
          "presentation": {
              "echo": true,
              "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
               "focus": false,
               "panel": "shared" // 不同的文件的编译信息共享一个终端面板
          },
      }
  ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:\\MinGW\\bin\\gCC.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "cppStandard": "c++17"
        }
    ],
    "version": 4
 }

c++三文件配置

Launch.json

{  
    "version": "0.2.0",  
 "configurations": [  
        {  
         "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
         "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
         "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
         "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
         "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
         "setupCommands": [  
                {   
		    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++", //这里注意一下,见下文
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-ggdb3",   // 生成和调试有关的信息
                "-Wall",    // 开启额外警告
                "-static-libgcc",   // 静态链接
                "-std=c++17",       // 使用c++17标准
                "-finput-charset=UTF-8",    //输入编译器文本编码 默认为UTF-8
                "-fexec-charset=GB18030",   //输出exe文件的编码
                "-D _USE_MATH_DEFINES"
            ],
            "options": {
                "cwd": "C:\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
                 "focus": false,
                 "panel": "shared" // 不同的文件的编译信息共享一个终端面板
            },
        }
    ]
}

c_cpp_properties.json


{
    "configurations": [
        {
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "D:\\Environment\\MinGW\\bin\\g++.exe",
            "includePath": [
                "${workspaceFolder}"
            ],
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
 }
 
### 配置 VSCode 支持 C/C++ 开发并使用 MinGW 进行编译 #### 安装 Visual Studio Code 和必要的扩展 安装好 Visual Studio Code 后,需通过 Extensions 市场来获取 `C/C++` 扩展包。该扩展由 Microsoft 提供,能够增强对 C/C++ 的语法高亮、智能感知等功能支持[^2]。 #### 下载与配置 MinGW-w64 编译器套件 对于 Windows 用户来说,推荐采用 MinGW-w64 来作为本地 GCC 工具链的选择。可以从官方网站下载适合系统的版本,并按照官方指引完成安装过程。安装完成后应确保将 MinGW-w64 的 bin 文件夹路径添加至系统的 PATH 环境变量中以便全局调用 gcc/g++ 编译命令[^1]。 #### 设置 VSCode 中的构建任务 打开项目文件夹下的 `.vscode/tasks.json` 文件(如果没有则创建),定义一个新的任务用于执行 g++/gcc 编译指令: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task to build a single file." } ] } ``` 此 JSON 片段描述了一个简单的构建任务,它会读取当前活动文档(`${file}`),利用 g++ 将其编译成同名但带有 .exe 后缀的目标程序[^3]。 #### 修改 launch.json 实现调试功能 为了让开发者可以在 VSCode 内部直接启动和调试应用程序,还需要编辑或新建 `.vscode/launch.json` 文件以指明调试适配器以及传递给它的参数: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/hello.exe", // 替换成实际生成的可执行文件路径 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "path/to/mingw/bin/gdb.exe", // 更改为 MinGW gdb 路径 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello world", "internalConsoleOptions": "openOnSessionStart" } ] } ``` 上述配置允许用户点击 F5 键即可触发预设的任务先编译再运行程序,并进入 GDB 调试模式。 #### 添加 c_cpp_properties.json 自定义 IntelliSense 模式 最后一步是在工作区根目录下建立名为`.vscode/c_cpp_properties.json` 的文件,用来告诉 C/C++ 插件在哪里查找头文件以及其他编译选项: ```json { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++"//根据实际情况调整路径 ], "defines": [], "compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe", //更改编译器路径 "intelliSenseMode": "clang-x64", "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" }, "cStandard": "c17", "cppStandard": "c++17" } ], "version": 4 } ``` 这段代码片段中的 includePath 字段应当指向 MinGW-w64 安装位置内的标准库头文件所在的位置;而 compilerPath 则要填写之前所提到过的已加入系统环境变量里的那个完整路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值