Using Clang in Visual Studio Code

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 按钮

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 文件的全部内容。

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果Autosar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值