VSCode配置c_cpp_properties.json文件

转载自:https://blog.csdn.net/fightfightfight/article/details/82857397

1.安装C/C++ for Visual Studio Code

点击左边扩展栏图标—>搜索C/C++ -> 安装->Reload:
在这里插入图片描述

安装完成之后,打开你的包含c++的文件夹,将会生成一个.vscode文件夹,所有的配置将在这个文件夹中进行配置。

2.配置IntelliSense

扩展程序会根据当前系统环境配置基本信息,因此有可能配置不完整,这时需要通过生成c_cpp_properties.json文件来配置缺少的信息:

ctrl+shift+P打开Command Palette,运行C/Cpp: Edit configurations...生成c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",        
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",  //编译器路径
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

  
  

    这是Ubuntu平台上默认生成的c_cpp_properties.json文件,可在这个文件中添加配置。

    对于Windows环境下,需要自己下载编译器安装并配置,比如我下载了MinGW64,然后需要配置环境变量:

    变量名:MINGW
    变量值:D:\worksoftware\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin
    引入到Path环境变量中:
    %MINGW%
    
      
      

    然后重启VS code,并在c_cpp_properties.json文件中添加:

    "compilerPath":  "D:\\worksoftware\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe"
    
      
      

    3.构建应用程序

    如果要构建应用程序,则需要生成tasks.json文件:

    Ctrl+Shift+P -> Tasks: Configure Tasks… -> Create tasks.json file from templates -> Others.

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build demo1",                                 //任务标签
                "type": "shell",                                             //类型
                "command": "g++",                                    //对应的命令:g++ -g demo1.cpp -o demo
                "args": [
                    "-g",
                    "demo1.cpp",
                    "-o",
                    "demo"
                ],
                "problemMatcher": [],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    
      
      

    生成tasks.json后,根据自己需求修改commandargs或其他字段。

    4.DEBUG代码

    要启用调试,需要生成launcher.json文件:

    点击菜单栏DEBUG->Add Configuration ->选择C++ (GDB/LLDB)(Windows下选择C++ Windows) ,这时将会生成launcher.json文件:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",   //正在使用的调试器,使用Visual Studio Windows时必须为cppvsdbg,使用GDB或LLDB时必须为cppdbg.
                "request": "launch",  //表示此配置是用于启动程序还是附加到已运行的实例上
                "program": "${workspaceFolder}/demo",   //要执行的可执行文件的完整路径
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",            //设置调试器启动的应用程序的工作目录
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",   //要连接的调试器
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    
      
      

    生成launcher.json文件后,其他的属性可不改,但必须将program属性的值修改为要执行的文件。然后点击Debug->Start Debugging,既可以开始调试了,点击侧边栏的Debug图标可查看BreakPoint、Call Stack等。

    参考文档

    VS Code 官方文档

    VS Code中,如果没有c_cpp_properties.json文件,你可以手动创建它。你可以按照以下步骤进行操作: 1. 打开VS Code,并在菜单栏中选择“文件”>“首选项”>“设置”。 2. 在设置页面的搜索栏中输入“C_Cpp.default.configurationProvider”并选择“编辑 in settings.json”。 3. 在settings.json文件中,添加以下内容: ``` "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" ``` 4. 保存并关闭settings.json文件。 5. 在VS Code中,按下Ctrl+Shift+P打开命令面板,然后输入“C/Cpp: Edit configurations...”并选择它。 6. 在弹出的窗口中,选择“Linux”配置,并将其内容替换为以下内容: ``` { "name": "Linux", "includePath": \[ "${workspaceFolder}/**" \], "defines": \[\], "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "linux-gcc-x64" } ``` 7. 保存并关闭c_cpp_properties.json文件。 现在,你应该在你的工作区中有一个c_cpp_properties.json文件,并且它包含了适合Linux环境的配置。请确保你的编译器路径和其他设置与你的实际环境相匹配。 #### 引用[.reference_title] - *1* *2* [VS code 配置C/C++环境](https://blog.csdn.net/FightFightFight/article/details/82857397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [liunx vscode没有c_cpp_properties.json文件](https://blog.csdn.net/Akali_L/article/details/129254593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
    评论 12
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值