VS Code 使用 clang++ 编译,使用 cppvsdbg 或 lldb 调试的配置方法

本文介绍了如何在VSCode中配置C/C++开发环境,包括安装必要的插件CodeLLDB,以及如何调整task.json、launch.json来使用cppdbg和lldb进行调试。c_cpp_properties.json的设置也进行了详细说明。
摘要由CSDN通过智能技术生成

需要安装的

VS Code

LLVM

VS Code 需要安装的插件:

C/C++(用来配置 c_cpp_properties.json)

CodeLLDB(如果你要用 lldb 调试,那么这个插件就需要安装,用来连接到 lldb 调试器)

流程

我们都知道配置编译器要设置三个 json,task, launch, c_cpp_properties.json

task.json 直接通过 terminal - configure default build task - C/C++: clang++.exe build active file 设置

launch.json 不再是通过 C/C++: clang++.exe build and debug active file 设置,这样得到的是使用 cppdbg 调试的,我试了调试会失败,只能使用 cppvsdbg 或 lldb 调试

要使用 cppvsdbg 的话,launch.json 如下:

{
    // Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
    // Pointez pour afficher la description des attributs existants.
    // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console":"newExternalWindow",
        }
    ]
}

其实也就是把 type 改了

如果要用 lldb 也就是把 type 改一下,然后要装 CodeLLDB 这个插件。只是有些选项不一样,所以不能用了

{
    // Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
    // Pointez pour afficher la description des attributs existants.
    // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal"
        }
    ]
}

c_cpp_properties.json 直接在 C/C++ 插件的设置中选择 clang++ 就好了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装 C++编译器,例如 GCC 或者 Clang。然后,你需要在 Visual Studio Code 中安装 C++ 插件,以便于编写、调试和运行 C++ 代码。 以下是具体的步骤: 1. 安装 C++ 编译器 - Windows 系统:可以下载 MinGW 或者 Cygwin,在安装时选择安装 C++ 编译器。 - Mac 系统:可以通过 Homebrew 安装 GCC 或者 Clang 编译器。在终端输入以下命令即可: ``` brew install gcc ``` 或者 ``` brew install llvm ``` - Linux 系统:可以通过包管理器安装 GCC 或者 Clang 编译器。 2. 安装 Visual Studio Code 你可以从官网上下载 Visual Studio Code 并安装。 3. 安装 C++ 插件 在 Visual Studio Code 中,打开 Extensions(快捷键为 Ctrl+Shift+X),搜索 C++,选择 Microsoft 的插件 C++,点击 Install 进行安装。 4. 配置编译器路径 在 Visual Studio Code 中打开一个 C++ 项目,按下 F1 键,输入 Edit Configurations(或者直接点击菜单栏中的 Run -> Add Configuration),选择 C++(GDB/LLDB)选项,然后在 launch.json 文件中添加以下配置: - 对于 MinGW 编译器: ``` { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "preLaunchTask": "C/C++: g++.exe build active file", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ``` 注意:如果你的 MinGW 安装在其他路径下,需要相应地修改 miDebuggerPath 选项。 - 对于 Clang 编译器: ``` { "name": "clang++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "lldb", "miDebuggerPath": "/usr/bin/lldb-mi", "preLaunchTask": "C/C++: clang++ build active file", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ``` 注意:如果你使用的是其他版本的 Clang 编译器,需要相应地修改 miDebuggerPath 选项。 5. 配置任务 在 Visual Studio Code 中按下 Ctrl+Shift+B 键,选择 Create tasks.json file from template -> Others,然后在 tasks.json 文件中添加以下配置: - 对于 MinGW 编译器: ``` { "version": "2.0.0", "tasks": [ { "label": "C/C++: g++.exe build active file", "type": "shell", "command": "g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] } ``` - 对于 Clang 编译器: ``` { "version": "2.0.0", "tasks": [ { "label": "C/C++: clang++ build active file", "type": "shell", "command": "clang++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] } ``` 6. 编写、调试和运行 C++ 代码 在 Visual Studio Code 中打开一个 C++ 项目,编写代码,并按下 F5 键进行调试或者按下 Ctrl+Shift+B 键进行编译。如果你使用的是 MinGW 编译器,按下 F5 键会打开一个命令行窗口,并运行编译后的可执行文件;如果你使用的是 Clang 编译器,按下 F5 键会在 Visual Studio Code 中直接运行编译后的可执行文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值