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.json
的includePath
和defines
。
除了以上两种方式以外,还有另一种方式:
指定compile_commands.json
- 让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
。
- 在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