自学内容网 自学内容网

【C++】使用vscode进行 C/C++ 开发,内含c_cpp_properties.json、launch.json 和 tasks.json解释

在这里插入图片描述

在 Visual Studio Code (VSCode) 中进行 C/C++ 开发时,这三个 .json 文件(c_cpp_properties.jsonlaunch.jsontasks.json)分别用于配置编译、调试和代码提示等功能。它们是 VSCode 配置环境的一部分,由 C/C++ 扩展生成,帮助你编写、编译、调试 C/C++ 程序。

1. c_cpp_properties.json

这个文件用于配置 C/C++ 代码的智能提示(IntelliSense)、编译器路径、包括的头文件路径等。它控制了 VSCode 中 C/C++ 代码的代码分析和自动补全功能。

常见的配置项:
  • includePath:指定头文件的搜索路径,VSCode 会根据这些路径提供代码补全和智能提示。
  • defines:定义编译时使用的宏。
  • compilerPath:指定编译器的路径,通常是 gccclang,用于解析代码。
  • intelliSenseMode:指定 VSCode 使用的 IntelliSense 模式,例如 gcc-x64clang-x64
示例 c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "C:/Program Files/MinGW/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
  • compilerPath:指定 C 编译器的路径(如 gcc)。
  • includePath:头文件的查找路径,VSCode 会根据这些路径为你提供代码补全。
  • intelliSenseMode:控制智能提示和代码解析器的行为,通常与编译器保持一致。

2. launch.json

launch.json 是调试配置文件,用于控制如何启动和调试 C/C++ 程序。它配置了调试器的运行环境,包括执行程序的路径、调试参数、调试类型等。

常见的配置项:
  • program:要调试的可执行文件路径。
  • args:传递给程序的命令行参数。
  • cwd:运行调试时的工作目录。
  • stopAtEntry:是否在程序入口点(如 main 函数)处停住,等待用户操作。
  • miDebuggerPath:调试器的路径,通常是 gdb
示例 launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "internalConsoleOptions": "openOnSessionStart"
        }
    ]
}
  • program:调试时要运行的可执行文件路径,例如 a.exe
  • miDebuggerPath:GDB 调试器的路径。
  • stopAtEntry:是否在程序入口处暂停。
  • preLaunchTask:调试前需要先运行的任务,比如自动编译。

3. tasks.json

tasks.json 文件用于配置编译任务。它告诉 VSCode 如何编译 C/C++ 代码。通过这个文件,你可以配置各种自动化任务,比如编译、清理项目等。

常见的配置项:
  • label:任务的名称,用于在其他文件中引用(如 launch.json 中的 preLaunchTask)。
  • command:执行的命令,例如 gcc
  • args:传递给命令的参数,例如编译选项和源文件。
  • group:任务的类型,可以是 buildtest
  • problemMatcher:用于匹配编译器输出中的错误信息,以便在 VSCode 中高亮错误。
示例 tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "gcc",
            "args": [
                "hello.c",
                "-o",
                "hello.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}
  • command:编译命令,这里是 gcc
  • args:编译的参数,hello.c 是源文件,-o hello.exe 是输出的可执行文件。
  • group:标记为构建任务。
  • problemMatcher:用来解析编译输出,自动标记出编译错误。

三个文件的关系:

  • c_cpp_properties.json:用于代码智能提示、头文件路径和编译器设置。
  • launch.json:用于配置调试参数,控制如何调试程序。
  • tasks.json:用于定义任务,比如编译 C 文件,可以通过编译任务生成可执行文件。

配置 C/C++ 开发环境:

  • 需要在 tasks.json 中配置好编译任务,确保可以使用 gcc 编译项目。
  • 然后在 launch.json 中配置调试,指定程序的路径和调试器的路径。
  • 最后,c_cpp_properties.json 可以帮助你设置头文件路径和智能提示,确保代码补全功能正常。

总结:

  • c_cpp_properties.json 用于代码智能提示的配置。
  • launch.json 用于调试的配置。
  • tasks.json 用于定义编译任务。

vscode没下载可以借鉴此佬~VSCODE的配置


原文地址:https://blog.csdn.net/weixin_44939430/article/details/142983042

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!