如何使用VSCode設定C語言環境

2021-05-07 10:01:18
本篇文章給大家介紹使用VSCode設定C語言環境的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

安裝相關外掛

打卡後進入如下介面,選擇這個C/C++的,然後點選install進行安裝,大概幾秒鐘就好了,安裝完成後install按鈕會變成uninstall(解除安裝):

安裝編譯器(MinGW-W64 GCC)

下載完成後解壓:

然後設定環境變數

找到這個資料夾內的一個叫bin的資料夾:

然後把它的地址複製一下,找到此電腦(或者我的電腦)——>右鍵——>屬性

然後進入到下面這個頁面,開啟高階系統設定:

在彈出的頁面中選擇「高階」分頁,找到環境變數,單擊開啟:

然後在環境變數中的系統變數中,找到Path變數:

開啟之後將剛剛複製的地址新增進去:


然後點確定,之前彈出的所有頁面都點選確定。然後測試環境設定是否成功:

crtl+R快捷鍵開啟執行視窗,在裡面輸入cmd,回車開啟cmd.exe

在這裡插入圖片描述
在cmd.exe中輸入如下命令:

gcc -v -E -x c++ -

如果執行結果像下方圖片中這樣,就設定成功了。

設定

最後在VSCode中進行相關設定:

先新建一個資料夾作為C語言專案檔案,然後點選選單欄中的File——>Open Folder,找到剛才新建的資料夾,然後點選選擇資料夾開啟這個專案檔案。

然後在裡面新建一個hello.c檔案(名字隨便起,以.c結尾就行了)


然後再建一個

.vscode資料夾(注意前面有個點),在裡面建三個檔案,c_cpp_properties.json、launch.json、tasks.json

  • c_cpp_properties.json:將這段程式碼複製進去
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/Program Files/mingw64/include/**",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "C:/Program Files/mingw64/include/**",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

然後,下方紅框裡的內容需要修改,將所有的 改為自己的安裝路徑,就是我們之前下載的編譯器的地址:

把你的MinGW-W64 GCC解壓後的檔案中的mingw64的地址複製下來,替換程式碼裡所有的 D:/Program Files (x86)/softwareFactory/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0/mingw64/ :

  • launch.json:複製貼上,然後miDebuggerPath屬性裡的內容也要改成自己的路徑
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole":true
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",// 自己電腦的gdb
            "preLaunchTask": "echo",//這裡和task.json的label相對應
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]

        }
    ]
}
  • tasks.json:複製貼上
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//解決中文亂碼
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}

然後就可以在之前建的hello.c檔案裡面寫程式啦,比如我們熟悉的hello world:

#include<stdio.h>
main()
{
    printf("hello world\n");
   
    //system("pause");
}

f5執行結果:

在這裡插入圖片描述

vscode設定c環境就設定完成。

推薦學習:《》

以上就是如何使用VSCode設定C語言環境的詳細內容,更多請關注TW511.COM其它相關文章!