Mac下爲VSCode配置C++環境


Mac下爲VSCode配置C++環境


編譯運行cpp文件

1. 安裝 Visual Studio Code on macOS
2. 在VSCode中安裝下圖所示插件,可在終端上輸入clang --version查看。

2-1
2-2

2-3

3. 挑選目錄並新建project文件夾,使用VSCode打開。如下圖選中LeetCode文件夾並打開

3-1

4. 在打開的project文件夾裏,新建cpp文件並寫入測試代碼。例如,新建helloworld.cpp,代碼如下:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

5. 在菜單欄,選擇Terminal > Configure Default Build Task,會生成一個task.json文件,修改其內容如下:

5-1

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "clang++ build active file",
			"command": "/usr/bin/clang++",
			"args": [
				"-std=c++17",
        		"-stdlib=libc++",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": ["$gcc"],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}
  • 這個task.json文件會編譯"args"中的"${file}"文件,並在${fileDirname}目錄下生成${fileBasenameNoExtension}文件,例如編譯本次helloworld.cpp會生成helloworld文件。
  • 可以使用 ${workspaceFolder}/*.cpp 代替 ${file},這會使其編譯現在project文件夾下的所有.cpp文件。
  • 可以使用 ${workspaceFolder}/myProgramName.out 代替 ${fileDirname}/${fileBasenameNoExtension},這會編譯生成你所期望的固定文件名。

6. 回到helloworld.cpp文件,按快捷鍵"⇧⌘B" 或點擊菜單欄中 Terminal > Run Build Task,會出現TERMINAL,並顯示編譯生成helloworld可執行文件的過程。再點擊右側的+按鈕,會生成現在工作目錄下的一個新終端,可以輸入ls查看該目錄下的所有文件,比如上一步生成的文件helloworldhelloworld.dSYM。在該終端下運行./helloworld,正常顯示如下如即可。

6-1

6-2

6-3


Debug .cpp文件

1. 菜單欄中先選擇Run > Add Configuration,再選擇C++ (GDB/LLDB),會生成一個lanch.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": "clang++ - Build and debug active file",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}",
          "args": [],
          "stopAtEntry": true,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "lldb",
          "preLaunchTask": "clang++ build active file"
        }
    ]
}

2. 菜單欄中選擇Run > Start Debugging,就可以開始debug你的.cpp文件了。其中,點擊Step Over可以一步一步運行代碼,在左側VARIABLES中可查看變量的變化情況,點擊Continue會直接運行完後續代碼。
2-1


C/C++ configuration

1. 使用快捷鍵 “⇧⌘P” 打開VSCode的命令端,搜索並選擇C/C++: Edit Configurations (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. MacOS 上的頭文件、庫文件都被 XCode 接管,也就是說不安裝 XCode 很多開發都是做不了的,但如果你不是一名 iOS 或 OS X 開發者,可以跳過安裝 XCode 的過程,直接安裝 Xcode command line tools.
可以從APP Store中下載XCode或者從官網下載安裝。本人從官網直接下載了Command_Line_Tools_macOS_10.14_for_Xcode_10.3.dmg(挑選符合自己Mac版本的)


至此全部配置結束

  • 現在已在macOS上爲VS Code配置使用Clang。 該配置適用於當前工作空間(project文件夾下)
  • 要重用配置,只需將.vscode文件夾複製到新的project文件夾(新工作區)下,然後根據需要更改.json文件中的源文件和可執行文件的名稱即可
  • 由於.vscode文件夾爲隱藏文件夾,在Mac中可以使用快捷鍵 ⇧⌘. 顯示隱藏文件

參考

  • 配置其他環境可在官方文檔中,自行尋找並按教程配置
  • 本文參考了Clang on macOS,成功爲Mac下的VSCode配置了C++環境
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章