~~ 記錄自己點點滴滴的學習歷程 ~~





C/C++:設定 VS Code 來編譯 C/C++ 語言

最近覺得 Python 寫多了,很多東西其實都省略掉了,為了好好加強自己的程式語言,決定來重學 C 語言。記得以前剛學程式的時候,都是用 Dev C++,不過現在有微軟的 VS Code,安裝容量也很輕量,另外還能安裝許多額外的擴充功能,我學很多程式都可以使用這個,實在太好用了,決定就用這個來當作自己學 C 語言的工具。


Download Files

Visual Studio Code - Code Editing. Redefined

MinGW-w64 - for 32 and 64 bit Windows download | SourceForge.net

LLVM Download Page

MinGW 安裝:我是載線上安裝版,安裝的時候我會將Architecture 更改為 x86_64,其餘不須更動,安裝路徑用預設的或自訂都可以,裝完之後再將裡面的 bin 資料夾加到環境變數中。




安裝 LLVM記得自己設定的安裝路徑,安裝完之後再將剛剛 MinGW 裡的東西複製到 LLVM 的資料夾,然後開啟 CMD,如果看到以下畫面代表成功,再來就可以設定 VS Code 了。


PS: 安裝 MinGW 應該可以用第四個檔案就好,載下來會是壓縮檔,解壓縮之後,再把所有檔案丟進去 LLVM 的資料夾中,應該也可以自動合併檔案。


VS Code Extensions

  • C/C++ (C/C++ 語言的支援)
  • C/C++ Clang Command Adapter (使用Clang命令完成 C / C ++)
  • Code Runner (運行程式文件)


設定編譯環境

建立 .vscode 資料夾,接著建立四個檔案分別為 c_cpp_properties.json、launch.json、setting.json、tasks.json。

c_cpp_properties.json

{
    "configurations": [
      {
        "name""MinGW",
        "intelliSenseMode""clang-x64",
        "compilerPath""D:/YJ/SDK/LLVM/bin/gcc.exe",
        "includePath": [
          "${workspaceFolder}"
        ],
        "defines": [],
        "browse": {
          "path": [
            "${workspaceFolder}"
          ],
          "limitSymbolsToIncludedHeaders"true,
          "databaseFilename"""
        },
        "cStandard""c11",
        "cppStandard""c++17"
      }
    ],
    "version"4
  }


launch.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""cl.exe - 建置及偵錯使用中的檔案",
            "type""cppvsdbg",
            "request""launch",
            "program""${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry"false,
            "cwd""${workspaceFolder}",
            "environment": [],
            "console""externalTerminal",
            "preLaunchTask""C/C++: cl.exe 建置使用中檔案"
        }
    ]
}


setting.json

{
    "files.defaultLanguage""cpp"
    "editor.formatOnType"true
    "editor.snippetSuggestions""top",
    "code-runner.runInTerminal"true,
    "code-runner.executorMap": {
      "c""cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
      "cpp""cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt"
    }, 
    "code-runner.saveFileBeforeRun"true
    "code-runner.preserveFocus"true
    "code-runner.clearPreviousOutput"false
    "C_Cpp.clang_format_sortIncludes"true,
    "C_Cpp.intelliSenseEngine""Default"
    "C_Cpp.errorSquiggles""Disabled"
    "C_Cpp.autocomplete""Disabled",
    "clang.cflags": [
      "--target=x86_64-w64-mingw",
      "-std=c11",
      "-Wall"
    ],
    "clang.cxxflags": [ 
      "--target=x86_64-w64-mingw",
      "-std=c++17",
      "-Wall"
    ],
    "clang.completion.enable"true 
  }


tasks.json

{
  "version""2.0.0",
  "tasks": [
    {
      "label""Compile"
      "command""clang++"
      "args": [
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe",
        "-g"
        "-Wall",
        "-static-libgcc",
        "-fcolor-diagnostics",
        "--target=x86_64-w64-mingw"
        "-std=c++17" 
      ], 
      "type""shell"
      "group": {
        "kind""build",
        "isDefault"true 
      },
      "presentation": {
        "echo"true,
        "reveal""always"
        "focus"false
        "panel""shared" 
      }
      // "problemMatcher":"$gcc" 
    }
  ]
}



參考

  1. 使用 VSCode 編譯並執行 C/C++ 語言 | JunYou's Blog
  2. Visual Studio Code 如何編寫運行 C、C++ 程序? - 每日頭條 (kknews.cc)
  3. VSCode編寫執行C、C++ 程式的方法 - tw511教學網
  4. 用 VSCode 寫 C/C++ 教學 - HackMD
  5. 使用 Visual Studio Code 寫 C/C++-黑暗執行緒 (darkthread.net)


留言

這個網誌中的熱門文章

Python:用 PyAutoGUI 來操控滑鼠及鍵盤

Arduino:SG90伺服馬達實作

MOD 接分享器:IPTV設定 (使用 Asus RT-AC51U)