C++环境配置

一个开源项目,构造好了C++项目的结构 https://github.com/Codesire-Deng/TemplateRepoCxx/

  • 在配环境的时候遇到一个很奇怪的问题,就是不管怎么配置都不能实时检测,最后是发现是因为settings.json文件中少了一行,“C_Cpp.intelliSenseEngine”: “default”,添加上一切奇怪的问题都解决了

在 VS Code 中,C++ 项目的配置主要涉及以下几个 JSON 文件,这些文件都应该在.vscode目录下:

  1. c_cpp_properties.json 用于配置 C++ 编译器、标准库路径和 IntelliSense 的相关信息。
  2. tasks.json 用于配置如何编译或运行 C++ 程序。
  3. launch.json 用于配置调试器。
  4. settings.json VS Code 的全局或工作区级别的设置文件,用于配置编译器路径等全局行为。

$ {workspaceFolder} 的主要作用是提供一种动态、可移植的方式来引用当前工作区的根目录路径,无需手动写入绝对路径。这对于在不同开发环境中共享项目配置非常有用。例如:如果你的工作区路径是 /home/user/project,${workspaceFolder} 会解析为 /home/user/project

要下载 C++ intellisense

c_cpp_properties.json: 通过 C/C++: Edit Configurations (UI) 创建。
tasks.json: 按 Ctrl+Shift+B 自动生成。
launch.json: 按 F5 选择调试环境后生成。
settings.json: 修改全局或工作区设置自动生成。

c_cpp_properties.json

这个文件主要为 C++ IntelliSense 提供头文件路径、编译器标准等信息。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include",
                "/usr/local/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

这个文件定义了如何编译和运行 C++ 项目。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build C++",
            "type": "shell",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${workspaceFolder}/output"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

launch.json

这个文件配置调试器(如 GDB 或 LLDB)。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug C++",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/output",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build C++"
        }
    ]
}

settings.json

VS Code 的全局或工作区级别配置。

{
  "workbench.colorTheme": "Default Dark+",
  "cmake.configureOnOpen": true,
  "window.zoomLevel": 0,
  "editor.minimap.enabled": true,
  "C_Cpp.autocomplete": "default",
  "[cpp]": {
      "editor.quickSuggestions": {
          "other": true,
          "comments": false,
          "strings": false
      }
  },
  "[c]": {
      "editor.quickSuggestions": {
          "other": true,
          "comments": false,
          "strings": false
      }
  },
  "C_Cpp.intelliSenseEngine": "default"
}