VSCode下載配置並運行C語言

前言 :筆者之前一直使用的Code::Blocks,但奈何麼有代碼提示啊 ! ! ! ,相比於其他的編程語言如Python、Java等,都有很友好的代碼提示,然而C卻沒有,然後通過百度發現了VSCode,嗯哼,看起來還不錯,但下載後配置真麻煩。

一、VS Code簡介

VS Code是微軟推出的一個跨平臺的開源編輯器。雖然它只是個編輯器但是隻要你願意折騰,還是能折騰出IDE(Integrated Development Environment ,集成開發環境)所具有的功能。

二、安裝VS Code

下載地址:https://code.visualstudio.com/Download

下載好VS Code後,安裝時可以自己選擇安裝路徑,其他的默認就好。

三、安裝Mingw-w64

本來沒打算安裝這個,但VSCode只是一個開發工具,提供編譯器,所以要想編譯C程序需要單獨下載。

下載地址:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

不推薦下載MinGW-W64-install.exe,筆者最開始也是下載的它,但可能因爲是牆的原因,很多庫都下載失敗,所以下載下面四個哪個都可以。

 下載並解壓後配置PATH:D:\English\VScode\mingw64\bin

 

 

四、安裝VS code插件

 

五、配置運行文件

新建一個工程,然後建一個.c文件,隨便寫點代碼,運行它,.vscode文件夾,文件夾下會生成launch.json文件,另外兩個可以通過在該文件夾下新建  tasks.json、c_cpp_properties.json,目錄結構如下圖

1、配置 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:\\English\\VScode\\mingw64\\bin\\gdb.exe", // miDebugger的路徑,注意這裏要與MinGw的路徑對應  
            "preLaunchTask": "g++", // 調試會話開始前執行的任務,一般爲編譯程序,c++爲g++, c爲gcc  
            "setupCommands": [  
                {   
            "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

2、配置 task.json文件

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 編譯命令參數
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceFolder}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

3、配置c_cpp_properties.json文件


{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\English\\VScode\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

 到此爲止如果應該就能運行成功了,如果運行時出現閃退,可以在.c文件中加入如下代碼:

#include <stdlib.h>

#include<stdio.h>

int main(){

    system("pause");

}

如有問題,歡迎評論 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章