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 } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

這是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%
  • 1
  • 2
  • 3
  • 4

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

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

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 } } ] } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

生成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 } ] } ] } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

生成launcher.json文件后,其他的屬性可不改,但必須將program屬性的值修改為要執行的文件。然后點擊Debug->Start Debugging,既可以開始調試了,點擊側邊欄的Debug圖標可查看BreakPoint、Call Stack等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM