vscode使用compile_commands.json

       

vscode使用compile_commands.json(转载)

源地址:vscode使用compile_commands.json - TruthHell - 博客园 (cnblogs.com)

背景

vscode+cmake可以实现C/C++项目开发和构建。可以在vscode上装以下几个插件:

CMake Tools插件能够给C/C++插件提供信息,实现IntelliSense、代码补全、注释浏览、文件转跳等功能。一般在第一次使用CMake Tools插件时会出现如下提示:

Allow之后会在当前工作目录的.vscode/settings.json文件(即当前工作目录的设置文件,会覆盖用户设置文件)中添加:

{
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}

当然,也可以在C/C++插件的配置文件.vscode/c_cpp_properties.json中手动指定configurationProvider

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        },
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/MinGW/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x86",
            "configurationProvider": "ms-vscode.cmake-tools",
        }
    ],
    "version": 4
}

这样C/C++插件就能正常工作了,不用自己指定.vscode/c_cpp_properties.jsonincludePathdefines
除了以上两种方式以外,还有另一种方式:

指定compile_commands.json

  1. 让cmake生成compile_commands.json,需要在CMakeLists.txt中添加set(CMAKE_EXPORT_COMPILE_COMMANDS ON)。例子:
set(CMAKE_SYSTEM_NAME Generic)
#cmake最低版本
cmake_minimum_required(VERSION 3.0.0)
#自动生成compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

会在build下生成compile_commands.json

  1. 在vscode中打开,配置.vscode/c_cpp_properties.json。指定compileCommands为上一步的${workspaceFolder}/build/compile_commands.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        },
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/MinGW/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x86",
            "configurationProvider": "ms-vscode.cmake-tools",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

这样和指定configurationProvider是一样的效果。

原理

  • configurationProvider

The ID of a VS Code extension that can provide IntelliSense configuration information for source files. For example, use the VS Code extension ID ms-vscode.cmake-tools to provide configuration information from the CMake Tools extension.

  • compileCommands

The full path to the compile_commands.json file for the workspace. The include paths and defines discovered in this file will be used instead of the values set for includePath and defines settings. If the compile commands database does not contain an entry for the translation unit that corresponds to the file you opened in the editor, then a warning message will appear and the extension will use the includePath and defines settings instead.

参考:

CMAKE_EXPORT_COMPILE_COMMANDS — CMake 3.26.4 Documentation
C++ programming with Visual Studio Code
Get started with CMake Tools on Linux
c_cpp_properties.json reference
Customize default settings in Visual Studio Code C++ projects
Configure IntelliSense for C++ cross-compilation
JSON Compilation Database Format Specification — Clang 17.0.0git documentation

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: c_cpp_properties.json 可以通过使用 CMake 或者其他构建工具自动生成。在 CMake 中,可以使用命令“cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1”生成 compile_commands.json 文件,然后使用工具“clangd”或“IntelliSense”将其转换为 c_cpp_properties.json 文件。在其他构建工具中,可以查看其文档以了解如何生成 c_cpp_properties.json 文件。 ### 回答2: c_cpp_properties.json文件是用于配置C/C++扩展的编辑器设置的。 在Visual Studio Code中,c_cpp_properties.json文件可以通过以下步骤进行自动生成: 1. 打开Visual Studio Code编辑器。 2. 在编辑器顶部的菜单栏中,选择“文件”>“首选项”>“设置”。 3. 在设置页面的搜索框中,输入“C_Cpp: Edit Configuration”并点击该设置选项。 4. 点击“编辑工作区设置”以打开当前工作区的设置文件。 5. 在设置文件中,使用下面的模板来定义c_cpp_properties.json文件的配置: ``` "c_cpp_properties": { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**" ], "macFrameworkPath": [ "/System/Library/Frameworks" ], "defines": [], "intelliSenseMode": "clang-x64" }, { "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "intelliSenseMode": "gcc-x64" }, { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "windowsSdkVersion": "10.0.19041.0", "intelliSenseMode": "msvc-x64" } ], "version": 4 } ``` 6. 根据不同的操作系统需求,修改配置中的路径和参数,例如在"Mac"部分修改"includePath"和"macFrameworkPath",在"Linux"部分修改"includePath"等等。 7. 保存设置文件后,Visual Studio Code将自动在当前工作区的根目录下生成c_cpp_properties.json文件,并将其用于C/C++扩展的设置。 通过按照以上步骤创建和修改c_cpp_properties.json文件,我们可以根据需求配置C/C++扩展的编辑器设置。 ### 回答3: c_cpp_properties.json是用于配置C/C++语言开发环境的配置文件,它可以用来指定编译器路径、头文件路径、库文件路径等信息。 c_cpp_properties.json的自动生成可以通过以下几种方式实现: 1. 使用VSCode的C/C++插件:VSCode的C/C++插件可以自动帮助我们生成c_cpp_properties.json文件。在打开C/C++项目时,插件会自动检测项目中的源代码,并根据代码中的依赖关系推测出编译器的路径、头文件路径和库文件路径,并将这些信息写入c_cpp_properties.json文件中。 2. 使用cmake工具:如果项目使用cmake进行构建,可以使用cmake自动生成c_cpp_properties.json文件。在CMakeLists.txt文件中,可以使用`configure_file`命令将模板的c_cpp_properties.json文件复制到项目的目标路径,并自动填充相关路径信息。 3. 手动编写配置文件:如果以上两种方式不能满足需求,可以手动编写c_cpp_properties.json文件。首先,需要根据编译环境和项目特点,确定编译器路径、头文件路径和库文件路径等信息,然后在该文件中进行手动配置。 无论选择哪种方式,最终都需要确保c_cpp_properties.json文件中包含正确的编译器路径、头文件路径和库文件路径等信息,以保证项目能够正确编译和运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值