Mac使用Visual Studio Code開發C++

在VS Code上使用Clang進行編譯和調試

  • 安裝Visual Studio Code

  • 安裝C++擴展插件

  • 安裝Clang插件

  • 在環境變量中添加code命令(如果已經配置了,跳過)

    • “Shift+Command+P”打開Command Palette
    • 輸入“Shell”,選擇“Shell Command: Install ‘code’ command in PATH”
    • 關閉VS Code
  • 在一個文件夾中啓動VS Code

    • 步驟
      mkdir cprojects
      cd cprojects
      mkdir cpptest
      cd cpptest
      code .
      
  • 創建3個json文件

    • c_cpp_properties.json指定編譯器的路徑

      • “Shift+Command+P”打開Command Palette
      • 輸入“C/C++”,選擇“Edit Configurations”
       {
       "configurations": [
           {
               "name": "macOS",
               "includePath": [
                   "${workspaceFolder}/**"
               ],
               "defines": [],
               "macFrameworkPath": [
                   "/System/Library/Frameworks",
                   "/Library/Frameworks"
               ],
               "compilerPath": "/usr/bin/clang",
               "cStandard": "c11",
               "cppStandard": "c++17",
               "intelliSenseMode": "clang-x64"
           }
       ],
       "version": 4
       }
      
    • tasks.json指定如何構建可執行文件

      • “Shift+Command+P”打開Command Palette
      • 輸入“task”,選擇“Tasks: Add a default build task”->“others
      {
      "version": "2.0.0",
      "tasks": [
          {
              "label": "Build with Clang",
              "type": "shell",
              "command": "clang++",
              "args": [
                  "-std=c++17",
                  "-stdlib=libc++",
                  "helloworld.cpp",
                  "-o",
                  "helloworld.out",
                  "--debug"
              ],
              "group": {
                  "kind": "build",
                  "isDefault": true
              }
          }
      ]
      }
      
    • launch.json進行調試器配置

       {
       "version": "0.2.0",
       "configurations": [
           {
               "name": "(lldb) Launch",
               "type": "cppdbg",
               "request": "launch",
               "program": "${workspaceFolder}/helloworld.out",
               "args": [],
               "stopAtEntry": true,
               "cwd": "${workspaceFolder}",
               "environment": [],
               "externalConsole": true,
               "MIMode": "lldb",
               "logging": {
                   "trace": true,
                   "traceResponse": true,
                   "engineLogging": true
               }
           }
       ]
       }
      
  • helloworld.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
        cout << "Hello World" <<endl;
        return 0;
    }
    
  • 編譯“Shift+Command+B”

  • 參考

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