笔记-vscode下的c_cpp_properties.json文件配置

本文详述了如何配置VSCode的c_cpp_properties.json文件,以实现C/C++插件的智能提示。重点讲解了配置文件的顶级属性、Configuration字段,特别是browse字段的设置,包括includePath、defines、browse.path等,以解决头文件查找和宏定义问题,提高代码编辑体验。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

这篇笔记的目的就是配置好c/c++插件代码的智能提示,方便以后写嵌入式代码的时候能够快速配置好
例子(来自官方文档)

{
  "env": {
    "myDefaultIncludePath": ["${workspaceFolder}", "${workspaceFolder}/include"],
    "myCompilerPath": "/usr/local/bin/gcc-7"
  },
  "configurations": [
    {
      "name": "Mac",
      "intelliSenseMode": "clang-x64",
      "includePath": ["${myDefaultIncludePath}", "/another/path"],
      "macFrameworkPath": ["/System/Library/Frameworks"],
      "defines": ["FOO", "BAR=100"],
      "forcedInclude": ["${workspaceFolder}/include/config.h"],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "compileCommands": "/path/to/compile_commands.json",
      "browse": {
        "path": ["${workspaceFolder}"],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}

顶级属性

env
用户自定义变量,可用于在其他配置属性中进行替换,在本例中myDefaultIncludePath即为用户自定义变量,在configurations.includePath字段下被引用
configurations
一组配置对象,向智能感知引擎提供有关你的项目和首选项的信息。默认情况下,扩展插件会根据操作系统自动创建相关信息,我们自定义配置主要就是修改这里
version
建议不要编辑这个字段,它跟踪c_cpp_properties.json文件的当前版本,以便扩展插件知道应该显示什么属性和设置,以及如何将该文件升级到最新版本。

Configuration字段

name
用来标识配置文件,一般是内核的名字就可以了,如"Linux"
compilerPath
用于构建项目的编译器的完整路径,例如/usr/bin/gcc,以启用更精确的智能感知。扩展将查询编译器,以确定系统包含的路径和用于智能感知的默认定义(网易翻译)
在交叉编译时,将该字段设置为编译器的绝对路径就行了,例如/root/esp8266/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc
compilerArgs
编译选项,之所以不重要,是因为defines的问题可以用browse解决,而include问题可以用includePath字段解决,该字段可以不写
intelliSenseMode
智能感知模式,有msvc-x64.gcc-x64和clang-x64,根据编译器的前端选择就行,例如我的xtensa编译器选的是gcc-x64
includePath(重要)
An include path is a folder that contains header files (such as #include “myHeaderFile.h”) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files. If a path ends with /** the IntelliSense engine will do a recursive search for header files starting from that directory. If on Windows with Visual Studio installed, or if a compiler is specified in the compilerPath setting, it is not necessary to list the system include paths in this list.(官方文档)
用这个在要包含的主目录后面加上**通配符就可以递归搜索,非常方便
defines
用于智能感知引擎在解析文件时使用的预处理程序定义的列表。可以选择使用=设置一个值,例如VERSION=1,我使用vscode的目的是为了代码的智能提示,并不是要实时检测代码的正确性,所以不必要将在编译时加上的宏定义在这里写上,用browse来自动搜索可用的宏定义就行了
cStandard
用于智能感知的C语言标准版本,根据实际情况确定
cppStandard
用于智能感知的c++语言标准的版本,根据实际情况确定
configurationProvider
用不到
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.
用不到,因为有browse
browse(重要)
The set of properties used when “C_Cpp.intelliSenseEngine” is set to “Tag Parser” (also referred to as “fuzzy” IntelliSense, or the “browse” engine). These properties are also used by the Go To Definition/Declaration features, or when the “Default” IntelliSense engine is unable to resolve the #includes in your source files(官方文档)
如果只是将需要包含的头文件放在includePath字段中,那么include的问题解决了,但是defines的问题还没有解决,这将会出现一大堆的提示,这些提示大部分都是因为缺少相应的宏定义引起的,而browse可以搜索相应browse.path字段中所有的宏定义,并把缺少的宏定义补全,让Definition/Declaration操作可以无障碍

browse字段

很少,只有三个
path
A list of paths for the Tag Parser to search for headers included by your source files. If omitted, includePath will be used as the path. Searching on these paths is recursive by default. Specify * to indicate non-recursive search. For example: /usr/include will search through all subdirectories while /usr/include/* will not(官方文档)
注意通配符问题,与includePath字段不相同
limitSymbolsToIncludedHeaders
When true, the Tag Parser will only parse code files that have been directly or indirectly included by a source file in ${workspaceFolder}. When false, the Tag Parser will parse all code files found in the paths specified in the browse.path list.(官方文档)
通常设置为true,如果有些代码没法智能提示可以将该字段设置为false试试
databaseFilename
用不上

实例

这是我现在在使用的配置文件

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/root/esp8266_3.2/ESP8266_RTOS_SDK-3.2/**"
            ],
            "defines": [],
            "compilerPath": "/root/esp8266/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "/root/esp8266_3.2/ESP8266_RTOS_SDK-3.2"
                ],
                "limitSymbolsToIncludedHeaders": true
            }
        }
    ],
    "version": 4
}

再一次说明 : 仅仅是为了代码提示,至于提示的质量不做任何保证

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值