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++环境
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章