vscode————c/c++环境配置

1、创建文件夹目录

Code        ->.vscode

                ->C

                ->C++

2、在.vscode文件夹下创建以四个名字命名的文件并添加内容

c_cpp_properties.json
launch.json
settings.json
tasks.json

        1)、c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Win64",
      "includePath": ["${workspaceFolder}/**"],
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/MinGW/bin/g++.exe",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "gcc-x64"
    }
  ],
  "version": 4
}

         2)、launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch", 
      "type": "cppdbg", 
      "request": "launch", 
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 
      "args": [], 
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true, 
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
      "preLaunchTask": "g++",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

          3)、settings.json

{
  "files.associations": {
    "*.py": "python",
    "iostream": "cpp",
    "*.tcc": "cpp",
    "string": "cpp",
    "unordered_map": "cpp",
    "vector": "cpp",
    "ostream": "cpp",
    "new": "cpp",
    "typeinfo": "cpp",
    "deque": "cpp",
    "initializer_list": "cpp",
    "iosfwd": "cpp",
    "fstream": "cpp",
    "sstream": "cpp",
    "map": "c",
    "stdio.h": "c",
    "algorithm": "cpp",
    "atomic": "cpp",
    "bit": "cpp",
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "compare": "cpp",
    "concepts": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "exception": "cpp",
    "ios": "cpp",
    "istream": "cpp",
    "iterator": "cpp",
    "limits": "cpp",
    "memory": "cpp",
    "random": "cpp",
    "set": "cpp",
    "stack": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "xfacet": "cpp",
    "xiosbase": "cpp",
    "xlocale": "cpp",
    "xlocinfo": "cpp",
    "xlocnum": "cpp",
    "xmemory": "cpp",
    "xstddef": "cpp",
    "xstring": "cpp",
    "xtr1common": "cpp",
    "xtree": "cpp",
    "xutility": "cpp",
    "stdlib.h": "c",
    "string.h": "c"
  },
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  "aiXcoder.showTrayIcon": true
}

          4)、tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "g++",
      "command": "g++",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe"
      ],
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

注意:在c_cpp_properties.json和launch.json文件中

"compilerPath": "C:/MinGW/bin/g++.exe",

"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",

将这两行里的路径改为自己编译器所在的路径即可。

### 配置 VSCode 使用 WSL 进行 C/C++ 开发 #### 工具准备 为了在 Windows 上使用 Linux 子系统 (WSL) 和 Visual Studio Code 构建高效的 C/C++ 编程环境,需先完成以下准备工作: - **安装并配置 WSL**: 可按照官方文档指导,在 Windows 中启用 WSL 功能,并选择合适的 Linux 发行版进行安装[^1]。 - **更新软件包列表及安装必要组件**: ```bash sudo apt update && sudo apt upgrade -y ``` - **安装 GCC/G++, GDB 调试器及其他依赖项**: ```bash sudo apt install -y build-essential gdb cmake ``` 以上操作确保了基础构建工具链的存在,这对于后续编写、编译和调试程序至关重要[^4]。 #### 设置 VSCode 支持 WSL 的工作区 一旦上述步骤顺利完成,则可以着手于集成 VSCode 来增强开发体验: - 打开 VSCode 并访问 Extensions 市场, 搜索 `Remote - WSL` 插件并点击 Install 安装该扩展。此功能允许开发者直接从编辑器内部启动基于 WSL 的终端会话,从而实现无缝衔接的工作流程[^2]。 - 推荐同时安装其他辅助性的插件如 "C/C++" by Microsoft 或者类似的语法高亮显示/智能感知服务提供者,以便更好地支持特定编程语言特性。 #### 创建项目结构与初始化 Git 版本控制 建议创建一个新的文件夹用于存放源码文件,并考虑初始化 git 库来跟踪变更历史记录: ```bash mkdir ~/my_cpp_project && cd $_ git init . ``` 接着可以通过右键菜单选项打开当前目录为新的 VSCode 实例窗口,此时应当自动切换至 WSL 环境模式运行。 #### 配置 launch.json 文件以简化调试过程 对于希望利用内置调试特性的用户来说,合理定制 `.vscode/launch.json` 是必不可少的一环。下面给出了一种常见的 JSON 配置模板,适用于单步执行简单的 C++ 控制台应用程序: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.out", // 输出二进制路径 "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" }, { "name": "Build and Debug Active File", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "compile active file" } ] } ``` 这段代码定义了一个名为 `(gdb) Launch` 的调试方案,它会在每次启动时尝试加载位于 `${workspaceFolder}` 下面最新生成的目标文件 (`a.out`);另外还设定了另一个更灵活的选择——即针对单独文件即时编译加调试的功能按钮 `"Build and Debug Active File"`[^3]。 #### 自动化任务管理 tasks.json 为了让日常编码更加高效便捷,还可以进一步完善项目的自动化脚本。比如通过修改或新增 `.vscode/tasks.json` 文件中的条目,使得一键式地触发某些常用命令成为可能。这里展示的是一个典型的例子,用来指定如何处理不同类型的编译请求: ```json { "tasks": [ { "label": "build", "command": "make all", "group": { "kind": "build", "isDefault": true }, "detail": "Compile the entire project using Make.", "problemMatcher": ["$gcc"], "presentation": {"echo": true,"reveal": "always","focus": false,"panel": "shared"} }, { "label": "compile active file", "type": "shell", "command": "g++", "args": [ "-g", "${relativeFile}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": false } } ], "version": "2.0.0" } ``` 此处展示了两个主要的任务标签:“build”负责调用 make 工具全量重建整个工程,“compile active file”则专注于仅对当前正在编辑的 .cpp/.cc 文件实施增量式的编译动作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值