Mac 配置VS code C++環境



image

0.下載VS code后安裝以下插件:

  • C\C++
  • C\C++ Clang Command Adapter
  • CodeLLDB(用來debug)
  • code runner (用來編譯)

安裝好插件后,要配置文件,也就是.vscode文件夾下的三個json文件。
(每次新建項目文件夾都要在對應的文件夾下配置一遍)

1.c_cpp_properties.json(編譯器路徑和智能感知設置)

shift+command+P -> C/C++ 編輯配置(JSON)配置文件c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

2. tasks.json(編譯器生成設置)

command+shift+B 進行編譯,此時我們還沒有要編譯的文件,這時會跳出來一個小窗口,一直點到other那里。這時有一個tasks.json生成。(如果不出來也可以手動創建,即直接在.vscode文件夾下創建一個然后把對應的內容粘貼進去保存) ,用以下代碼替換生成的task.json文件。

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Build with Clang",
        "type": "shell",
        "command": "clang++",
        "args": [
          "${file}",
                "-std=c++11",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                "-g",
                "--debug"
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

3.launch.json(調試器設置)

image
然后選LLDB
image

4.測試

#include<iostream>
using namespace std;

int main(){
    cout<<"hello,c++"<<endl;
    return 0;
}

然后就可以愉快地運行cpp文件啦。

(但是用cin的話程序會一直運行但是也找不到輸入的地方)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM