vscode如何配置C語言環境
第一步:
首先,先下載visual studio code下載,然後安裝,安裝過程就自行百度吧,漢化過程也請自行百度。安裝完成打開vscode,進入到如下界面:
接下來,點擊左側第五個按鈕如圖所示:
在箭頭的地方輸入c/c++安裝,我這裏已經安裝好了,然後接着下載MinGW,請下載64位的下載x86_64.seh版本的,如果直接下載.exe很可能由於網絡問題無法安裝,然後直接解壓到自己喜歡的文件夾,並且配置環境變量,環境變量的配置我這裏配置了兩個,一個就是在系統環境變量path中加上解壓後的MinGW的路徑,即到bin的路徑(例如:D:\app\setup\mingw\install\mingw64\bin),還有一個是INCLUDE環境變量,首席按察可能自己的系統環境變量裏有沒有INCLUDE,如果沒有就新建一個,然後把mingw的include路徑加進去,(例如:D:\app\setup\mingw\install\mingw64\include),如下圖所示:
配置完畢,要驗證一下是否配置成功,快捷鍵win+R,win鍵是ctrl鍵和alt鍵之間的鍵,然後在裏賣弄輸入cmd。按下enter鍵,然後在窗口裏輸入gcc -v,如果如下圖所示,即成功了:
接下來我們重啓vscode,
首先要創建一個文件夾,是用來放自己編寫的項目的,然後用vscode打開這個文件夾,並在裏面創建.vscode文件夾,並在vscode中創建三個json文件,內容如下:launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名稱,將會在啓動配置的下拉菜單中顯示
"type": "cppdbg", // 配置類型,這裏只能爲cppdbg
"request": "launch", // 請求配置類型,可以爲launch(啓動)或attach(附加)
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 將要進行調試的程序的路徑
"args": [], // 程序調試時傳遞給程序的命令行參數,一般設爲空即可
"stopAtEntry": false, // 設爲true時程序將暫停在程序入口處,一般設置爲false
"cwd": "${workspaceFolder}", // 調試程序時的工作目錄,一般爲${workspaceFolder}即代碼所在目錄
"environment": [],
"externalConsole": true, // 調試時是否顯示控制檯窗口,一般設置爲true顯示控制檯
"MIMode": "gdb",
"miDebuggerPath": "D:\\app\\setup\\mingw\\install\\mingw64\\bin\\gdb.exe", // miDebugger的路徑,注意這裏要與MinGw的路徑對應
"preLaunchTask": "gcc", // 調試會話開始前執行的任務,一般爲編譯程序,c++爲g++, c爲gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
其中miDebuggerPath是你自己的MinGW中的gdb.exe路徑,
tasks.json的內容如下:
{
// 有關 tasks.json 格式的參考文檔:https://go.microsoft.com/fwlink/?LinkId=733558 。
"version": "2.0.0",
"tasks": [{
"label": "gcc",
"type": "shell", // { shell | process }
// 適用於 Windows 的配置:
"windows": {
"command": "gcc",
"args": [
"-g",
"\"${file}\"",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
// 設置編譯後的可執行文件的字符集爲 GB2312:
// "-fexec-charset", "GB2312"
// 直接設置命令行字符集爲 utf-8:
// chcp 65001
]
},
// 定義此任務屬於的執行組:
"group": {
"kind": "build", // { build | test }
"isDefault": true // { true | false }
},
// 定義如何在用戶界面中處理任務輸出:
"presentation": {
// 控制是否顯示運行此任務的面板。默認值爲 "always":
// - always: 總是在此任務執行時顯示終端。
// - never: 不要在此任務執行時顯示終端。
// - silent: 僅在任務沒有關聯問題匹配程序且在執行時發生錯誤時顯示終端
"reveal": "silent",
// 控制面板是否獲取焦點。默認值爲 "false":
"focus": false,
// 控制是否將執行的命令顯示到面板中。默認值爲“true”:
"echo": false,
// 控制是否在任務間共享面板。同一個任務使用相同面板還是每次運行時新創建一個面板:
// - shared: 終端被共享,其他任務運行的輸出被添加到同一個終端。
// - dedicated: 執行同一個任務,則使用同一個終端,執行不同任務,則使用不同終端。
// - new: 任務的每次執行都使用一個新的終端。
"panel": "dedicated"
},
// 使用問題匹配器處理任務輸出:
"problemMatcher": {
// 代碼內問題的所有者爲 cpp 語言服務。
"owner": "cpp",
// 定義應如何解釋問題面板中報告的文件名
"fileLocation": [
"relative",
"${workspaceFolder}"
],
// 在輸出中匹配問題的實際模式。
"pattern": {
// The regular expression.
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
// 第一個匹配組匹配文件的相對文件名:
"file": 1,
// 第二個匹配組匹配問題出現的行:
"line": 2,
// 第三個匹配組匹配問題出現的列:
"column": 3,
// 第四個匹配組匹配問題的嚴重性,如果忽略,所有問題都被捕獲爲錯誤:
"severity": 4,
// 第五個匹配組匹配消息:
"message": 5
}
}
}]
}
最後一個是setting.json,如下所示:
{
"files.associations": {
"tidl_alg_int.h": "c",
"limits": "c"
}
}
保存完畢,然後關閉vscode,重啓vscode。然後在.vscode的上一級文件夾創建一個C語言編寫的``程序,如下圖所示:
c程序編寫成功,點擊圖中右上角小三角即可運行,然後再圖中的下方的終端即可看到運行結果,也可點擊F5直接debug運行。
以下爲圖中的示例代碼:是一個小遊戲,如果運行成功,即代表C語言環境安裝成功:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#define MAX 100
long long int speed = 0;//控制敵機的速度
int position_x, position_y;//飛機的所在位置
int high, width;//地圖的大小
int bullet_x, bullet_y;//子彈的位置
int enemy_x, enemy_y;//敵人的位置
int map[MAX][MAX];
/*0表示空白,1表示戰機*的區域,2表示敵人戰機的位置。
3表示上下圍牆,4表示左右圍牆,5表示子彈的位置*/
int score;
void starup()//初始化所有的信息
{
high = 20;
width = 30;
position_x = high / 2;
position_y = width / 2;
bullet_x = 0;
bullet_y = position_y;
enemy_x = 2;
enemy_y = position_y - 1;
score = 0;
}
void startMap()
{
int i, j;
for (i = 1; i <= high - 1; i++)
{
map[i][1] = 4;
for (j = 2; j <= width - 1; j++)
map[i][j] = 0;
map[i][width] = 4;
}
//下方圍牆的初始化
i = high;
for (j = 1; j <= width; j++)
map[i][j] = 3;
map[bullet_x][bullet_y] = 5;
/*這裏是戰機大小的初始化開始*/
map[position_x - 1][position_y] = 1;
i = position_x;
for (j = position_y - 2; j <= position_y + 2; j++)
map[i][j] = 1;
map[position_x + 1][position_y - 1] = 1;
map[position_x + 1][position_y + 1] = 1;
/*** 初始化結束 **/
/* 敵人戰機的初始化 */
map[enemy_x][enemy_y] = 2;
map[enemy_x - 1][enemy_y - 1] = 2;
map[enemy_x - 1][enemy_y + 1] = 2;
/* 敵人戰機初始化結束*/
}
void HideCursor()//隱藏光標
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)//清理一部分屏幕
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void updateWithoutInput()//於輸入無關的跟新
{
if (bullet_x > 0)
bullet_x--;
if ((bullet_x == enemy_x) && (bullet_y == enemy_y))//當敵人的飛機被擊中時
{
score++;
enemy_x = 0;
enemy_y = rand() % width;
bullet_x = 0;
}
if (enemy_x > high)//當飛機超出區域
{
enemy_x = 0;
enemy_y = rand() % width;
}
if (speed == 1)
for (int i = 1; i <= 10000; i++)//用來控制敵機的速度
{
for (int j = 1; j <= 1000; j++)
{
speed = 1;
}
}
speed = 0;
if (speed == 0)
{
enemy_x++;
speed = 1;
}
}
void updateWithInput()//與輸入有關的更新
{
char input;
if (kbhit())//在VC6.0++下,爲_kbhit()
{
input = getch();//在VC6.0++下爲_getch();
if (input == 'a')
position_y--;
if (input == 's')
position_x++;
if (input == 'd')
position_y++;
if (input == 'w')
position_x--;
if (input == ' ')
{
bullet_x = position_x - 1;
bullet_y = position_y;
}
}
}
void show()//展示的內容
{
gotoxy(0, 0);
int i, j;
for (i = 1; i <= high; i++)
{
for (j = 1; j <= width; j++)
{
if (map[i][j] == 0)
printf(" ");
if (map[i][j] == 1)
printf("*");
if (map[i][j] == 2)
printf("#");
if (map[i][j] == 3)
printf("~");
if (map[i][j] == 4)
printf("|");
if (map[i][j] == 5)
printf("|");
}
printf("\n");
}
printf("\n你的得分:%d\n\n", score);
printf("操作說明: ASDW分別操作 左下右上四個的移動\n");
printf("**空格是發出子彈**\n");
}
int main()
{
starup();
while (1)
{
HideCursor();
startMap();
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}