Using Clang in Visual Studio Code
In this tutorial, you configure Visual Studio Code on macOS to use the Clang/LLVM compiler and debugger.
After configuring VS Code, you will compile and debug a C++ program in VS Code. This tutorial does not teach you about Clang or the C++ language. For those subjects, there are many good resources available on the Web.
Prerequisites
To successfully complete this tutorial, you must do the following steps:
Install Visual Studio Code on macOS.(Drag Visual Studio Code.app to the Applications folder, making it available in the macOS Launchpad, then Open the Command Palette (Cmd+Shift+P) and type ‘shell command’ to find the Shell Command: Install ‘code’ command in PATH command.)
Install the C++ extension for VS Code. You can install the C/C++ extension by searching for ‘C++’ in the Extensions view (⇧⌘X).
Ensure Clang is installed
Clang might already be installed on your Mac. To verify that it is, open a macOS Terminal window and enter the following command:
clang --version
If Clang isn’t installed, enter the following command to install the command line developer tools, which include Clang:
xcode-select --install
Create Hello World app
From the macOS Terminal, create an empty folder called projects where you can store all your VS Code projects, then create a subfolder called helloworld, navigate into it, and open VS Code in that folder by entering the following commands in the terminal window:
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
The code . command opens VS Code in the current working folder, which becomes your “workspace”. As you go through the tutorial, three files are created in the .vscode folder in your workspace:
tasks.json (compiler build settings)
launch.json (debugger settings)
c_cpp_properties.json (compiler path and IntelliSense settings)
添加 hello world 源代码文件
如果尚未配置 IntelliSense,请打开命令面板(⇧⌘P)并输入Select IntelliSense Configuration。从编译器下拉列表中,选择Use clang++进行配置。
运行 helloworld.cpp
Understanding tasks.json
The first time you run your program, the C++ extension creates tasks.json, located in your project’s .vscode folder. tasks.json stores build configurations.
Here is a sample of a tasks.json file on macOS:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
- command 设置指定要运行的程序,在这种情况下是 clang++。
- args 数组指定要传递给 clang++的命令行参数,这些参数必须按照编译器期望的顺序指定。 这个任务告诉 C++ 编译器:取当前活动文件 ( f i l e ) , 编译它 , 并在当前目录 ( {file}),编译它,并在当前目录 ( file),编译它,并在当前目录({fileDirname}) 中创建一个输出文件 (-o开关),文件名与活动文件相同但没有文件扩展名 (${fileBasenameNoExtension})。这个过程会创建helloworld 文件
- label 值是您在任务列表中看到的内容,这取决于您的个人偏好。
- detail 值是任务列表中的任务描述,可以更新这个字符串以区分不同的任务。
- problemMatcher值选择用于在编译器输出中查找错误和警告的输出解析器。对于 clang++来说,$gcc 问题匹配器效果最好。
- 从现在开始,播放按钮将读取 tasks.json 来确定如何构建和运行程序。我们可以在 tasks.json中定义多个构建任务,并将其中一个任务标记为默认任务,这个任务就会被播放按钮使用。如果需要更改默认编译器,可以运行"任务:配置默认构建任务"命令,或者直接修改tasks.json 文件并删除默认设置。
"group": {
"kind": "build",
"isDefault": true
},
with this:
"group": "build",
Modifying tasks.json
Debug helloworld.cpp
To debug your code,
Go back to helloworld.cpp
so that it is the active file.
Set a breakpoint by clicking on the editor margin or using F9 on the current line.
You will see the task execute and print out the output to the Terminal window.
Explore the debugger
Before you start stepping through the code, let’s take a moment to notice several changes in the user interface:
The Integrated Terminal appears at the bottom of the source code editor. In the Debug Console tab, you see output that indicates the debugger is up and running.
集成终端出现在源代码编辑器的底部。在“调试控制台”选项卡中,您会看到指示调试器已启动并正在运行的输出。
Step through the code
现在,您可以开始逐步执行代码了。
在调试控制面板中选择“Step over”图标,以便突出显示 for (const string& word : msg) 语句。
Step Over 命令会跳过在创建和初始化 msg 变量时调用的 vector 和 string 类中的所有内部函数调用。请注意“变量”窗口中的变化。msg 的内容可见,因为该语句已完成。
再次按 Step over 前进到下一个语句(跳过为初始化循环而执行的所有内部代码)。现在,“变量”窗口显示有关循环变量的信息。
再次按 Step over 执行 cout 语句。
如果您愿意,可以继续按 Step over,直到 vector 中的所有单词都已打印到控制台。但如果您很好奇,请尝试按 Step Into 按钮逐步执行 C++ 标准库中的源代码!
Customize debugging with launch.json
按F进行debug调试:
VS Code creates a launch.json file, which looks something like this:
Adding additional C/C++ settings
You only need to modify the Include path setting if your program includes header files that are not in your workspace or the standard library path.
只有当您的程序包含不在工作区或标准库路径中的头文件时,您才需要修改 Include 路径设置。
Compiler path
该扩展使用 compilerPath 设置来推断 C++ 标准库头文件的路径。当扩展知道在哪里可以找到这些文件时,它就可以提供诸如智能代码补全和转到定义等功能。
C/C++ 扩展尝试根据在您系统上找到的内容来填充 compilerPath。compilerPath 搜索顺序如下:
- 在您的 PATH 中查找已知编译器的名称。编译器在列表中出现的顺序取决于您的 PATH。
- 然后搜索硬编码的 Xcode 路径,如 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/
Mac framework path
在 C/C++ 配置屏幕上,向下滚动并展开"高级设置",确保 Mac 框架路径指向系统头文件。比如: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
这是针对 macOS 系统的配置说明。在 C/C++ 配置中,需要正确设置 Mac 框架路径,以便 IntelliSense 能够正确定位和使用系统框架头文件。将该路径设置为 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks 可以确保 IntelliSense 能够正常工作。这对于提供正确的代码补全和导航功能非常重要。
Compiler and linking errors
最常见的错误原因(例如未定义的 _main,或尝试链接为未知的不支持的文件格式构建的文件等)是当您开始构建或开始调试时 helloworld.cpp 不是活动文件时发生的。这是因为编译器正在尝试编译非源代码的内容,例如 launch.json、tasks.json 或 c_cpp_properties.json 文件。
如果您看到构建错误提到“C++11 扩展”,则您可能没有更新您的 task.json 构建任务以使用 clang++ 参数 --std=c++17。默认情况下,clang++ 使用 C++98 标准,该标准不支持 helloworld.cpp 中使用的初始化。请确保用“运行 helloworld.cpp”部分中提供的代码块替换您的 task.json 文件的全部内容。