1. 程式人生 > 實用技巧 >vscode簡答搭建C / C++ 環境

vscode簡答搭建C / C++ 環境

0 環境

  • 系統:win10
  • 編輯器: vscode

1 相關安裝

vscode下載

MinGW下載

若想要MinGW最新版本,往下拉,找到最新版的 "x86_64-posix-seh",點選下載就行,不需要的話,直接下載我上面提供的即可

vscode c外掛下載
參考圖 Path第一次裝需要新建

按 win + R,輸入cmd,回車鍵後輸入g++ -v/gcc -v,再回車,如果提示以下資訊gcc version啥的,則環境變數配置成功,否則重新配置環境變數。

2 vscode配置

1 安裝目錄以及demo下載

c目錄

vscode c環境搭建demo git下載地址

vscode c環境搭建demo git下載地址

2 環境配置

由於網上有很多的教程 只寫重點 可參考別人的文章 1.新建1.cpp檔案

#include <stdio.h>
#include <windows.h>

int main()
{
 printf("Hello World\n");
 system("pause");
 return 0;
}

2.進入除錯介面新增配置環境,選擇C++(GDB/LLDB),再選擇g++.exe,之後會自動生成launch.json 配置檔案 3.launch.json 配置檔案

{
    "version": "0.2.0",
    "configurations"
: [ { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole"
: true, //修改此項,讓其彈出終端 "MIMode": "gdb", "miDebuggerPath": "D:\\install\\mingw\\bin\\gdb.exe", // 你電腦裡mingw gdb的真實位置 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: gcc.exe build active file" } ] }

4.返回.cpp檔案,按F5進行除錯,會彈出找不到任務"task g++",選擇 "配置任務",會自動生成 tasks.json 檔案編輯 tasks.json 檔案

{
 "version": "2.0.0",
 "tasks": [
  {
   "type": "shell",
   "label": "C/C++: gcc.exe build active file",
   "command": "D:\\install\\mingw\\bin\\g++.exe", // mingw的真實路徑 需自己對應修改
   "args": [
    "-g",
    "${file}",
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
   ],
   "options": {
    "cwd": "D:\\install\\mingw\\bin" // mingw的真實路徑 需自己對應修改
   },
   "problemMatcher": [
    "$gcc"
   ],
   "group": "build"
  }
 ]
   }

5.launch.json 檔案中 "preLaunchTask" 的值 必須與 tasks.json 檔案中 "label"的值一致。值的設定看個人喜好,保持預設也是OK的。