這篇博客記錄一下c_cpp_properties.json文件的基本設置以及各個字段的含義。首先看一下官網給的例子:
{ "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
用不上