Neovim中clangd无法索引指定头文件的问题求助
问题:clangd无法识别compile_commands.json中已配置的头文件
问题详情
我有一个通过CMake构建的大型C项目,已生成compile_commands.json且所有包含路径都已配置,但在main.c中clangd无法找到libxml/parser.h。compile_commands.json里,main.c的编译命令包含-I[path/to/project]/sdk/include参数,而libxml/parser.h正好位于该目录下。
使用环境:
- Neovim v0.7.2
- Ubuntu clangd version 12.0.0-3ubuntu1~20.04.5
此前用ccls时也遇到同样问题,推测是项目规模过大导致(包含1000条编译命令,每条长度达5700字符)。
Neovim中clangd相关配置
-- Use an on_attach function to only map the following keys -- after the language server attaches to the current buffer local on_attach = function(client, bufnr) -- Enable completion triggered by <c-x><c-o> vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') -- Mappings. -- See `:help vim.lsp.*` for documentation on any of the below functions local bufopts = { noremap=true, silent=true, buffer=bufnr } vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts) vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts) vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts) vim.keymap.set('n', '<space>wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, bufopts) vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts) vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts) vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts) vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) vim.keymap.set('n', '<alt>L', vim.lsp.buf.formatting, bufopts) end local lsp_flags = { -- This is the default in Nvim 0.7+ debounce_text_changes = 150, } require('lspconfig')['pyright'].setup{ on_attach = on_attach, flags = lsp_flags, } require('lspconfig')['tsserver'].setup{ on_attach = on_attach, flags = lsp_flags, } require('lspconfig')['rust_analyzer'].setup{ on_attach = on_attach, flags = lsp_flags, -- Server-specific settings... settings = { ["rust-analyzer"] = {} } } require('lspconfig').clangd.setup{ on_attach = on_attach, flags = lsp_flags, cmd = {"clangd-12"} }
Clangd日志信息
非verbose模式日志
[START][2022-08-12 06:28:15] LSP logging initiated [ERROR][2022-08-12 06:28:18] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:18.108] Ubuntu clangd version 12.0.0-3ubuntu1~20.04.5\nI[06:28:18.108] PID: 185\n" [ERROR][2022-08-12 06:28:18] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:18.109] Working directory: /mnt/c/Users/gianl/Desktop/ite_sdk\nI[06:28:18.109] argv[0]: clangd-12\n" [ERROR][2022-08-12 06:28:18] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:18.109] Starting LSP over stdin/stdout\n" [ERROR][2022-08-12 06:28:18] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:18.109] <-- initialize(1)\n" [ERROR][2022-08-12 06:28:18] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:18.134] --> reply:initialize(1) 24 ms\n" [ERROR][2022-08-12 06:28:33] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:33.828] <-- initialized\n" [ERROR][2022-08-12 06:28:33] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:33.828] <-- textDocument/didOpen\n" [ERROR][2022-08-12 06:28:33] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:33.857] Loaded compilation database from /mnt/c/Users/gianl/Desktop/ite_sdk/compile_commands.json\n" [ERROR][2022-08-12 06:28:33] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:33.857] ASTWorker building file /mnt/c/Users/gianl/Desktop/ite_sdk/project/standard_project/main.c version 0 with command clangd fallback\n[/mnt/c/Users/gianl/Desktop/ite_sdk/project/standard_project]\n/usr/bin/clang /mnt/c/Users/gianl/Desktop/ite_sdk/project/standard_project/main.c -fsyntax-only -resource-dir=/usr/lib/llvm-12/lib/clang/12.0.0\n" [ERROR][2022-08-12 06:28:33] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "I[06:28:33.997] --> textDocument/publishDiagnostics\n"
旧日志中的错误条目
[ERROR][2022-08-12 06:25:57] .../vim/lsp/rpc.lua:420 "rpc" "clangd-12" "stderr" "V[06:25:57.138] Dex query tree: (LIMIT 10000 (& T=lex T=exi T=pio T=tpi T=itp T=xit T=oct T=ctl T=tle T=ioc S= ?=Restricted For Code Completion))\n"
内容的提问来源于stack exchange,提问作者Gian Laager
相关产品推荐
相关产品推荐

