VSCode C/C++ Intellisense 不适用于 CMake 项目
VSCode C/C++ Intellisense not working with CMake project
我正在尝试使用 libTooling 编写一个工具。我设置了它,以便它可以使用 LLVM 文档中的示例进行编译。但是 C/C++ Intellisense 似乎不适用于 CMake 项目。
我的工具位于:
<project>/clang-tools-extra/mytool
现在 C/C++ 扩展试图读取 compile_config.json 并告诉我找不到 <project>/build/compile_config.json
,而是使用 c_cpp_properties.json
中的 includePath
.
我尝试在工作区设置中手动添加包含路径:
{
"C_Cpp.default.includePath": [
"../../",
"../../clang/Frontend/",
"../../clang/Tooling/",
"../../llvm/Support/"
],
"C_Cpp.default.browse.path": [
"../.."
]
}
或在文件中 c_cpp_properties.json
。但它仍然在错误的位置搜索包含。例如。其中包括:
#include "llvm/Support/CommandLine.h"
它试图在 <project>/llvm/include/llvm/Support/CommandLine.h
中找到。所以很明显它从 command_config.json 读取了一些东西,即使它说它找不到它(当它在那里时),但是错误的东西。它根本不应该添加 llvm/include
。
1) 将“"compileCommands"”指向'compile_commands.json'
2) 您可能想添加 **,例如:../../**
双星使其递归
3) 您可以使用一些变量,例如 ${workspaceRoot}
而不是在包含路径
中使用相对路径
如果其他人有这个问题,我可以通过从 MSVC 完成切换到 clang 来解决它。
使用 clang 它实际上给了我工具提示,显示路径是如何错误的。
在此处查看如何将智能感知模式和位置设置为 clang:
https://code.visualstudio.com/docs/languages/cpp
也许您只需要启用扩展 cmake-tools,正确配置 CMakeLists.txt
,然后 运行 cmake。
一般情况下,只要cmake没有错误,Intellisense应该可以正常工作。
您需要安装 cmake-tool 扩展 (ms-vscode.cmake-tools) 并将以下配置添加到您的 c_cpp_properties.json
:
{
"configurations": [
{
"compileCommands": "${workspaceFolder}/_build/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
对我有用
对于 VSCode 1.63+(2022 或更高版本):
1. 安装 CMake Tools 扩展 (ms-vscode.cmake-tools
).
2. 把这个放在 .vscode/c_cpp_properties.json
:
{
"configurations": [
{
"name": "CMake",
"compileCommands": "${config:cmake.buildDirectory}/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
我正在尝试使用 libTooling 编写一个工具。我设置了它,以便它可以使用 LLVM 文档中的示例进行编译。但是 C/C++ Intellisense 似乎不适用于 CMake 项目。
我的工具位于:
<project>/clang-tools-extra/mytool
现在 C/C++ 扩展试图读取 compile_config.json 并告诉我找不到 <project>/build/compile_config.json
,而是使用 c_cpp_properties.json
中的 includePath
.
我尝试在工作区设置中手动添加包含路径:
{
"C_Cpp.default.includePath": [
"../../",
"../../clang/Frontend/",
"../../clang/Tooling/",
"../../llvm/Support/"
],
"C_Cpp.default.browse.path": [
"../.."
]
}
或在文件中 c_cpp_properties.json
。但它仍然在错误的位置搜索包含。例如。其中包括:
#include "llvm/Support/CommandLine.h"
它试图在 <project>/llvm/include/llvm/Support/CommandLine.h
中找到。所以很明显它从 command_config.json 读取了一些东西,即使它说它找不到它(当它在那里时),但是错误的东西。它根本不应该添加 llvm/include
。
1) 将“"compileCommands"”指向'compile_commands.json'
2) 您可能想添加 **,例如:../../**
双星使其递归
3) 您可以使用一些变量,例如 ${workspaceRoot}
而不是在包含路径
如果其他人有这个问题,我可以通过从 MSVC 完成切换到 clang 来解决它。
使用 clang 它实际上给了我工具提示,显示路径是如何错误的。
在此处查看如何将智能感知模式和位置设置为 clang: https://code.visualstudio.com/docs/languages/cpp
也许您只需要启用扩展 cmake-tools,正确配置 CMakeLists.txt
,然后 运行 cmake。
一般情况下,只要cmake没有错误,Intellisense应该可以正常工作。
您需要安装 cmake-tool 扩展 (ms-vscode.cmake-tools) 并将以下配置添加到您的 c_cpp_properties.json
:
{
"configurations": [
{
"compileCommands": "${workspaceFolder}/_build/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
对我有用
对于 VSCode 1.63+(2022 或更高版本):
1. 安装 CMake Tools 扩展 (ms-vscode.cmake-tools
).
2. 把这个放在 .vscode/c_cpp_properties.json
:
{
"configurations": [
{
"name": "CMake",
"compileCommands": "${config:cmake.buildDirectory}/compile_commands.json",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}