首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >clangd不会自动找到`compile_commands.json`

clangd不会自动找到`compile_commands.json`
EN

Stack Overflow用户
提问于 2022-08-30 16:56:28
回答 1查看 342关注 0票数 0

设置

我正在使用clangd通过neovimnvim-lspconfig

代码语言:javascript
复制
require('lspconfig').clangd.setup {
    -- on_attach = keybinds.on_attach,
    cmd = {
        "clangd",
        "--background-index",
        "--suggest-missing-includes",
        -- "--compile-commands-dir=/home/localuser/test/build",
    },
    filetypes = { "c", "cpp", "objc", "objcpp" },
}

问题

clangd没有找到特定于项目的包含。

它应该做什么

根据文档:(https://clangd.llvm.org/installation.html)

clangd将查找您编辑的文件的父目录,以及名为build/的子目录。例如,如果编辑$SRC/gui/window.cpp,我们在$SRC/gui/$SRC/gui/build/$SRC/$SRC/build/、…中搜索

我用cmake标志-DCMAKE_EXPORT_COMPILE_COMMANDS=1构建了这个项目,在build目录中有compile_commands.json~/test/build/compile_commands.json

我正在编辑的源文件是~/test/src/main.cpp

根据文档,clangd应该从build文件夹中找到编译数据库,因为build文件夹位于源文件的父文件夹中,但它没有。

发生了什么

clangd给出了包含错误。最后我已经包含了完整的~/.local/state/nvim/lsp.log

  • I可以显式地向clangd显示带有--compile-commands-dir标志的JSON (在第一个代码块中注释),然后它就可以工作了。然而,这是一个系统范围的配置,所以我不想这样做(根据我不应该这么做的文档)
  • ,我也可以将./build/compile_commands.json符号链接到./compile_commands.json,然后clangd正确工作。

日志

我创造了一个小例子。它基本上只是一个main()函数,其中包括opencv库(在CMakeLists.txt中是必需的),这个项目编译得很好,但是clangd抱怨说找不到opencv2/opencv.hpp

下面是测试项目目录布局:

代码语言:javascript
复制
localadmin@workstation:~/test$ tree
.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.16.3
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   ├── TargetDirectories.txt
│   │   └── test.dir
│   │       ├── build.make
│   │       ├── cmake_clean.cmake
│   │       ├── CXX.includecache
│   │       ├── DependInfo.cmake
│   │       ├── depend.internal
│   │       ├── depend.make
│   │       ├── flags.make
│   │       ├── link.txt
│   │       ├── progress.make
│   │       └── src
│   │           └── main.cpp.o
│   ├── cmake_install.cmake
│   ├── compile_commands.json
│   ├── Makefile
│   └── test
├── build.sh
├── CMakeLists.txt
└── src
    └── main.cpp

这是完整的lsp.log

代码语言:javascript
复制
[START][2022-10-24 14:16:22] LSP logging initiated
[INFO][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:662 "Starting RPC client"   { args = { "--background-index", "--suggest-missing-includes" }, cmd = "clangd", extra = { cwd = "/home/localadmin/test" }}
[TRACE][2022-10-24 14:16:22] .../lua/vim/lsp.lua:1283   "LSP[clangd]"   "initialize_params" { capabilities = { callHierarchy = { dynamicRegistration = false }, offsetEncoding = { "utf-8", "utf-16" }, textDocument = { codeAction = { codeActionLiteralSupport = { codeActionKind = { valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" } } }, dataSupport = true, dynamicRegistration = false, isPreferredSupport = true, resolveSupport = { properties = { "edit" } } }, completion = { completionItem = { commitCharactersSupport = false, deprecatedSupport = false, documentationFormat = { "markdown", "plaintext" }, preselectSupport = false, snippetSupport = false }, completionItemKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 } }, contextSupport = false, dynamicRegistration = false, editsNearCursor = true }, declaration = { linkSupport = true }, definition = { linkSupport = true }, documentHighlight = { dynamicRegistration = false }, documentSymbol = { dynamicRegistration = false, hierarchicalDocumentSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, hover = { contentFormat = { "markdown", "plaintext" }, dynamicRegistration = false }, implementation = { linkSupport = true }, publishDiagnostics = { relatedInformation = true, tagSupport = { valueSet = { 1, 2 } } }, references = { dynamicRegistration = false }, rename = { dynamicRegistration = false, prepareSupport = true }, signatureHelp = { dynamicRegistration = false, signatureInformation = { activeParameterSupport = true, documentationFormat = { "markdown", "plaintext" }, parameterInformation = { labelOffsetSupport = true } } }, synchronization = { didSave = true, dynamicRegistration = false, willSave = false, willSaveWaitUntil = false }, typeDefinition = { linkSupport = true } }, window = { showDocument = { support = false }, showMessage = { messageActionItem = { additionalPropertiesSupport = false } }, workDoneProgress = true }, workspace = { applyEdit = true, configuration = true, symbol = { dynamicRegistration = false, hierarchicalWorkspaceSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, workspaceEdit = { resourceOperations = { "rename", "create", "delete" } }, workspaceFolders = true } }, clientInfo = { name = "Neovim", version = "0.8.0" }, initializationOptions = vim.empty_dict(), processId = 377762, rootPath = "/home/localadmin/test", rootUri = "file:///home/localadmin/test", trace = "off", workspaceFolders = { { name = "/home/localadmin/test", uri = "file:///home/localadmin/test" } }}
[DEBUG][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:285    "rpc.send"  { id = 1, jsonrpc = "2.0", method = "initialize", params = { capabilities = { callHierarchy = { dynamicRegistration = false }, offsetEncoding = { "utf-8", "utf-16" }, textDocument = { codeAction = { codeActionLiteralSupport = { codeActionKind = { valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" } } }, dataSupport = true, dynamicRegistration = false, isPreferredSupport = true, resolveSupport = { properties = { "edit" } } }, completion = { completionItem = { commitCharactersSupport = false, deprecatedSupport = false, documentationFormat = { "markdown", "plaintext" }, preselectSupport = false, snippetSupport = false }, completionItemKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 } }, contextSupport = false, dynamicRegistration = false, editsNearCursor = true }, declaration = { linkSupport = true }, definition = { linkSupport = true }, documentHighlight = { dynamicRegistration = false }, documentSymbol = { dynamicRegistration = false, hierarchicalDocumentSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, hover = { contentFormat = { "markdown", "plaintext" }, dynamicRegistration = false }, implementation = { linkSupport = true }, publishDiagnostics = { relatedInformation = true, tagSupport = { valueSet = { 1, 2 } } }, references = { dynamicRegistration = false }, rename = { dynamicRegistration = false, prepareSupport = true }, signatureHelp = { dynamicRegistration = false, signatureInformation = { activeParameterSupport = true, documentationFormat = { "markdown", "plaintext" }, parameterInformation = { labelOffsetSupport = true } } }, synchronization = { didSave = true, dynamicRegistration = false, willSave = false, willSaveWaitUntil = false }, typeDefinition = { linkSupport = true } }, window = { showDocument = { support = false }, showMessage = { messageActionItem = { additionalPropertiesSupport = false } }, workDoneProgress = true }, workspace = { applyEdit = true, configuration = true, symbol = { dynamicRegistration = false, hierarchicalWorkspaceSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, workspaceEdit = { resourceOperations = { "rename", "create", "delete" } }, workspaceFolders = true } }, clientInfo = { name = "Neovim", version = "0.8.0" }, initializationOptions = vim.empty_dict(), processId = 377762, rootPath = "/home/localadmin/test", rootUri = "file:///home/localadmin/test", trace = "off", workspaceFolders = { { name = "/home/localadmin/test", uri = "file:///home/localadmin/test" } } }}
[ERROR][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:734    "rpc"   "clangd"    "stderr"    "I[14:16:22.583] clangd version 10.0.0-4ubuntu1 
I[14:16:22.583] PID: 377766
I[14:16:22.583] Working directory: /home/localadmin/test
I[14:16:22.583] argv[0]: clangd
I[14:16:22.583] argv[1]: --background-index
I[14:16:22.583] argv[2]: --suggest-missing-includes
I[14:16:22.583] Starting LSP over stdin/stdout
I[14:16:22.584] <-- initialize(1)
I[14:16:22.584] --> reply:initialize(1) 0 ms
"
[DEBUG][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:388    "rpc.receive"   { id = 1, jsonrpc = "2.0", result = { capabilities = { codeActionProvider = { codeActionKinds = { "quickfix", "refactor", "info" } }, completionProvider = { resolveProvider = false, triggerCharacters = { ".", ">", ":" } }, declarationProvider = true, definitionProvider = true, documentFormattingProvider = true, documentHighlightProvider = true, documentLinkProvider = { resolveProvider = false }, documentOnTypeFormattingProvider = { firstTriggerCharacter = "
", moreTriggerCharacter = {} }, documentRangeFormattingProvider = true, documentSymbolProvider = true, executeCommandProvider = { commands = { "clangd.applyFix", "clangd.applyTweak" } }, hoverProvider = true, referencesProvider = true, renameProvider = { prepareProvider = true }, selectionRangeProvider = true, signatureHelpProvider = { triggerCharacters = { "(", "," } }, textDocumentSync = 2, typeHierarchyProvider = true, workspaceSymbolProvider = true }, offsetEncoding = "utf-8" }}
[DEBUG][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:285    "rpc.send"  { jsonrpc = "2.0", method = "initialized", params = vim.empty_dict()}
[INFO][2022-10-24 14:16:22] .../lua/vim/lsp.lua:1343    "LSP[clangd]"   "server_capabilities"   { server_capabilities = { codeActionProvider = { codeActionKinds = { "quickfix", "refactor", "info" } }, completionProvider = { resolveProvider = false, triggerCharacters = { ".", ">", ":" } }, declarationProvider = true, definitionProvider = true, documentFormattingProvider = true, documentHighlightProvider = true, documentLinkProvider = { resolveProvider = false }, documentOnTypeFormattingProvider = { firstTriggerCharacter = "
", moreTriggerCharacter = {} }, documentRangeFormattingProvider = true, documentSymbolProvider = true, executeCommandProvider = { commands = { "clangd.applyFix", "clangd.applyTweak" } }, hoverProvider = true, referencesProvider = true, renameProvider = { prepareProvider = true }, selectionRangeProvider = true, signatureHelpProvider = { triggerCharacters = { "(", "," } }, textDocumentSync = { change = 2, openClose = true, save = { includeText = false }, willSave = false, willSaveWaitUntil = false }, typeHierarchyProvider = true, workspaceSymbolProvider = true }}
[DEBUG][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:285    "rpc.send"  { jsonrpc = "2.0", method = "textDocument/didOpen", params = { textDocument = { languageId = "cpp", text = '#include <iostream>
#include "opencv2/opencv.hpp"

int main(int argc, char** argv ) {

 if(argc != 2)
 {
 std::cerr << std::endl << "Usage: ./datacollector";
 std::cerr << " dataset_save_path" << std::endl;
 // std::cerr << " path_to_settings"; 
 // std::cerr << " path_to_ros2bag_1";
 // std::cerr << " (path_to_ros2bag_2 ... path_to_ros2bag_N) " << std::endl;
 return 1;
 }
}
', uri = "file:///home/localadmin/test/src/main.cpp", version = 0 } }}
[ERROR][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:734    "rpc"   "clangd"    "stderr"    "I[14:16:22.723] <-- initialized
I[14:16:22.723] unhandled notification initialized
I[14:16:22.723] <-- textDocument/didOpen
"
[ERROR][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:734    "rpc"   "clangd"    "stderr"    "I[14:16:22.724] Failed to find compilation database for /home/localadmin/test/src/main.cpp
I[14:16:22.724] Updating file /home/localadmin/test/src/main.cpp with command clangd fallback
[/home/localadmin/test/src]
/usr/lib/llvm-10/bin/clang /home/localadmin/test/src/main.cpp -fsyntax-only -resource-dir=/usr/lib/llvm-10/lib/clang/10.0.0
"
[ERROR][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:734    "rpc"   "clangd"    "stderr"    "I[14:16:22.994] --> textDocument/publishDiagnostics
"
[DEBUG][2022-10-24 14:16:22] .../vim/lsp/rpc.lua:388    "rpc.receive"   { jsonrpc = "2.0", method = "textDocument/publishDiagnostics", params = { diagnostics = { { code = "pp_file_not_found", message = "'opencv2/opencv.hpp' file not found", range = { ["end"] = { character = 29, line = 1 }, start = { character = 9, line = 1 } }, relatedInformation = {}, severity = 1, source = "clang" } }, uri = "file:///home/localadmin/test/src/main.cpp" }}
[TRACE][2022-10-24 14:16:22] .../lua/vim/lsp.lua:1052   "notification"  "textDocument/publishDiagnostics"   { diagnostics = { { code = "pp_file_not_found", message = "'opencv2/opencv.hpp' file not found", range = { ["end"] = { character = 29, line = 1 }, start = { character = 9, line = 1 } }, relatedInformation = {}, severity = 1, source = "clang" } }, uri = "file:///home/localadmin/test/src/main.cpp"}
[TRACE][2022-10-24 14:16:22] ...lsp/handlers.lua:519    "default_handler"   "textDocument/publishDiagnostics"   { ctx = '{
 client_id = 1,
 method = "textDocument/publishDiagnostics"
}', result = { diagnostics = { { code = "pp_file_not_found", message = "'opencv2/opencv.hpp' file not found", range = { ["end"] = { character = 29, line = 1 }, start = { character = 9, line = 1 } }, relatedInformation = {}, severity = 1, source = "clang" } }, uri = "file:///home/localadmin/test/src/main.cpp" }}
[INFO][2022-10-24 14:16:29] .../lua/vim/lsp.lua:1814    "exit_handler"  { { _on_attach = <function 1>, attached_buffers = { true }, cancel_request = <function 2>, commands = {}, config = { _on_attach = <function 3>, autostart = true, capabilities = { callHierarchy = { dynamicRegistration = false }, offsetEncoding = { "utf-8", "utf-16" }, textDocument = { codeAction = { codeActionLiteralSupport = { codeActionKind = { valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.rewrite", "source", "source.organizeImports" } } }, dataSupport = true, dynamicRegistration = false, isPreferredSupport = true, resolveSupport = { properties = { "edit" } } }, completion = { completionItem = { commitCharactersSupport = false, deprecatedSupport = false, documentationFormat = { "markdown", "plaintext" }, preselectSupport = false, snippetSupport = false }, completionItemKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 } }, contextSupport = false, dynamicRegistration = false, editsNearCursor = true }, declaration = { linkSupport = true }, definition = { linkSupport = true }, documentHighlight = { dynamicRegistration = false }, documentSymbol = { dynamicRegistration = false, hierarchicalDocumentSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, hover = { contentFormat = { "markdown", "plaintext" }, dynamicRegistration = false }, implementation = { linkSupport = true }, publishDiagnostics = { relatedInformation = true, tagSupport = { valueSet = { 1, 2 } } }, references = { dynamicRegistration = false }, rename = { dynamicRegistration = false, prepareSupport = true }, signatureHelp = { dynamicRegistration = false, signatureInformation = { activeParameterSupport = true, documentationFormat = { "markdown", "plaintext" }, parameterInformation = { labelOffsetSupport = true } } }, synchronization = { didSave = true, dynamicRegistration = false, willSave = false, willSaveWaitUntil = false }, typeDefinition = { linkSupport = true } }, window = { showDocument = { support = false }, showMessage = { messageActionItem = { additionalPropertiesSupport = false } }, workDoneProgress = true }, workspace = { applyEdit = true, configuration = true, symbol = { dynamicRegistration = false, hierarchicalWorkspaceSymbolSupport = true, symbolKind = { valueSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } } }, workspaceEdit = { resourceOperations = { "rename", "create", "delete" } }, workspaceFolders = true } }, cmd = { "clangd", "--background-index", "--suggest-missing-includes" }, cmd_cwd = "/home/localadmin/test", filetypes = { "c", "cpp", "objc", "objcpp" }, flags = {}, get_language_id = <function 4>, handlers = <1>{}, init_options = vim.empty_dict(), log_level = 2, message_level = 2, name = "clangd", on_attach = <function 5>, on_exit = <function 6>, on_init = <function 7>, root_dir = "/home/localadmin/test", settings = vim.empty_dict(), single_file_support = true, workspace_folders = <2>{ { name = "/home/localadmin/test", uri = "file:///home/localadmin/test" } }, <metatable> = <3>{ __tostring = <function 8> } }, handlers = <table 1>, id = 1, initialized = true, is_stopped = <function 9>, messages = { messages = {}, name = "clangd", progress = {}, status = {} }, name = "clangd", notify = <function 10>, offset_encoding = "utf-8", request = <function 11>, request_sync = <function 12>, requests = {}, rpc = { is_closing = <function 13>, notify = <function 14>, request = <function 15>, terminate = <function 16> }, server_capabilities = { codeActionProvider = { codeActionKinds = { "quickfix", "refactor", "info" } }, completionProvider = { resolveProvider = false, triggerCharacters = { ".", ">", ":" } }, declarationProvider = true, definitionProvider = true, documentFormattingProvider = true, documentHighlightProvider = true, documentLinkProvider = { resolveProvider = false }, documentOnTypeFormattingProvider = { firstTriggerCharacter = "
", moreTriggerCharacter = {} }, documentRangeFormattingProvider = true, documentSymbolProvider = true, executeCommandProvider = { commands = { "clangd.applyFix", "clangd.applyTweak" } }, hoverProvider = true, referencesProvider = true, renameProvider = { prepareProvider = true }, selectionRangeProvider = true, signatureHelpProvider = { triggerCharacters = { "(", "," } }, textDocumentSync = { change = 2, openClose = true, save = { includeText = false }, willSave = false, willSaveWaitUntil = false }, typeHierarchyProvider = true, workspaceSymbolProvider = true }, stop = <function 17>, supports_method = <function 18>, workspaceFolders = <table 2>, workspace_did_change_configuration = <function 19>, workspace_folders = <table 2>, <metatable> = { __index = <function 20> } } }
[DEBUG][2022-10-24 14:16:29] .../vim/lsp/rpc.lua:285    "rpc.send"  { id = 2, jsonrpc = "2.0", method = "shutdown"}
[ERROR][2022-10-24 14:16:30] .../vim/lsp/rpc.lua:734    "rpc"   "clangd"    "stderr"    "I[14:16:30.000] <-- shutdown(2)
I[14:16:30.000] --> reply:shutdown(2) 0 ms
"
[DEBUG][2022-10-24 14:16:30] .../vim/lsp/rpc.lua:388    "rpc.receive"   { id = 2, jsonrpc = "2.0"}
[DEBUG][2022-10-24 14:16:30] .../vim/lsp/rpc.lua:285    "rpc.send"  { jsonrpc = "2.0", method = "exit"}
[START][2022-10-24 14:18:47] LSP logging initiated
[INFO][2022-10-24 14:18:47] .../lua/vim/lsp.lua:1814    "exit_handler"  {} 
EN

回答 1

Stack Overflow用户

发布于 2022-08-30 20:33:22

给定文件/home/localuser/Projects/SLAM3/Examples/Stereo-Inertial/stereo_inertial_bag.cc,,clangd将查看的位置如下:

代码语言:javascript
复制
/home/localuser/Projects/SLAM3/Examples/Stereo-Inertial/compile_commands.json
/home/localuser/Projects/SLAM3/Examples/Stereo-Inertial/build/compile_commands.json
/home/localuser/Projects/SLAM3/Examples/compile_commands.json
/home/localuser/Projects/SLAM3/Examples/build/compile_commands.json
/home/localuser/Projects/SLAM3/compile_commands.json
/home/localuser/Projects/SLAM3/build/compile_commands.json
/home/localuser/Projects/compile_commands.json
/home/localuser/Projects/build/compile_commands.json
etc.

它不查看/home/localuser/SLAM3/MyProject/build,因为源文件不在MyProject中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73546064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档