简体   繁体   English

YouCompleteMe 对于 C 头文件不能正常工作

[英]YouCompleteMe not work properly for C headers

I was writing a small c program for learning system calls, and found out that YouCompleteMe in my vim can not give correct completion for functions like sigaction in C headers.我正在编写一个用于学习系统调用的小型 c 程序,发现 vim 中的 YouCompleteMe 无法正确完成 C 头文件中的sigaction函数。 This is the code header I included.这是我包含的代码头。

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>

and when I type siga , the completion panel is like this:当我输入 siga 时,完成面板是这样的: 在此处输入图片说明
It does not give the correct completion for sigaction function, I guess it's something wrong about my flags setting in .ycm_extra_conf.py file.它没有为sigaction函数提供正确的完成,我想我在 .ycm_extra_conf.py 文件中的标志设置有问题。 This is .ycm_extra_conf.py file for my C program:这是我的 C 程序的 .ycm_extra_conf.py 文件:

import os
import ycm_core

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c99',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x', 'c',
'-isystem', '/usr/local/include',
'-isystem', '/usr/include/x86_64-linux-gnu',
'-isystem', '/usr/include'
]

This is YcmDebugInfo:这是 YcmDebugInfo:

Printing YouCompleteMe debug information...
-- Resolve completions: Never
-- Client logfile: /tmp/ycm_3yj6gesj.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.8.2
-- Server has Clang support compiled in: False
-- Clang version: None
-- Extra configuration file found and loaded
-- Extra configuration path: /home/zxy/understanding-unix-linux-programming/ch7/.ycm_extra_conf.py
-- C-family completer debug information:
--   Clangd running
--   Clangd process ID: 177623
--   Clangd executable: ['/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_
party/ycmd/third_party/clang/lib/clang/10.0.0', '-limit-results=500']
--   Clangd logfiles:
--     /tmp/clangd_stderrtwgnq048.log
--   Clangd Server State: Initialized
--   Clangd Project Directory: /home/zxy/understanding-unix-linux-programming/ch7
--   Clangd Settings: {}
--   Clangd Compilation Command: ['clang-tool', '-Wall', '-Wextra', '-Werror', '-fexceptions', '-DNDEBUG', '-std=c99', '-x', 'c', '-isystem', '/usr/local/include', '-isystem', '/usr/include/x86_64-linux-gnu', '-
isystem', '/usr/include', '/home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c']
-- Server running at: http://127.0.0.1:55187
-- Server process ID: 177581
-- Server logfiles:
--   /tmp/ycmd_55187_stdout_235pm2ty.log
--   /tmp/ycmd_55187_stderr_pd5sgbpp.log

And this is from one of YcmToggleLogs files:这是来自 YcmToggleLogs 文件之一:

I[17:12:54.491] clangd version 10.0.0 (https://github.com/ycm-core/llvm 038587147cf2f97d1c3e677042f69560c65b5bea)
I[17:12:54.491] PID: 177542
I[17:12:54.491] Working directory: /home/zxy/understanding-unix-linux-programming/ch7
I[17:12:54.491] argv[0]: /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
I[17:12:54.491] argv[1]: -header-insertion-decorators=0
I[17:12:54.491] argv[2]: -resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0
I[17:12:54.491] argv[3]: -limit-results=500
I[17:12:54.491] Starting LSP over stdin/stdout
I[17:12:54.497] <-- initialize(1)
I[17:12:54.497] --> reply:initialize(1) 0 ms
I[17:12:54.498] <-- initialized
I[17:12:54.498] unhandled notification initialized
I[17:12:54.498] <-- workspace/didChangeConfiguration
I[17:12:54.499] <-- workspace/didChangeConfiguration
I[17:12:54.499] Failed to find compilation database for /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c
I[17:12:54.499] <-- textDocument/didOpen
I[17:12:54.500] Updating file /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c with command
[/home/zxy/understanding-unix-linux-programming/ch7]
clang-tool -Wall -Wextra -Werror -fexceptions -DNDEBUG -std=c99 -x c -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c -fsyntax-only -resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0
I[17:12:54.501] Enqueueing 1 commands for indexing
E[17:12:54.501] Tried to create storage for empty directory!
I[17:12:54.559] --> textDocument/publishDiagnostics
I[17:12:54.592] Indexed /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c (601 symbols, 1916 refs, 43 files)
I[17:12:54.592] Failed to compile /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c, index may be incomplete
E[17:12:54.593] Failed to write background-index shard for file /usr/include/features.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0/include/stdbool.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0/include/stddef.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/clockid_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /usr/include/stdlib.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/timer_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/stdc-predef.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/gnu/stubs-64.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/sys/types.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/sys_errlist.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/floatn.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/unistd.h: atomic_write_error: failed_to_create_uniq_file                                                                              E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/FILE.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/unistd_ext.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/gnu/stubs.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/timesize.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/libc-header-start.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/signal.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/stdio_lim.h: atomic_write_error: failed_to_create_uniq_file                                                     E[17:12:54.599] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/signal_ext.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.599] Failed to write background-index shard for file /usr/include/stdio.h: atomic_write_error: failed_to_create_uniq_file                                                                               E[17:12:54.599] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/typesizes.h: atomic_write_error: failed_to_create_uniq_file

but if I configure .ycm_extra_conf.py file for cpp,但是如果我为 cpp 配置 .ycm_extra_conf.py 文件,

import os
import ycm_core

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x', 'c++',
'-isystem/usr/include/c++/9',
'-isystem/usr/include/x86_64-linux-gnu',
'-isystem/usr/local/include',
'-isystem/usr/include'
]

the completion works ok without changing my code :在不更改我的代码的情况下完成工作正常: 在此处输入图片说明

can someone help me on this?有人可以帮我吗? I've been searching whole day, still no solultion...找了一整天,还是没有解决

This is an old question but I would like to provide my suggestion for future reference.这是一个老问题,但我想提供我的建议以供将来参考。 I had similar problems with youcompleteme as well.我有类似的问题, youcompleteme为好。 After carefully reading the project README , I learned that the preferred way to configure youcompleteme for c family of languages is to use the compilation database .仔细阅读项目README后,我了解到为c系列语言配置youcompleteme的首选方法是使用compilation database You can generate the compile_commands.json ( compilation database in json format) using your build tool and place it in the root directory of your project.您可以使用构建工具生成compile_commands.json (json 格式的compilation database )并将其放在项目的根目录中。 youcompleteme will pickup the compilation database automatically when you open vim on the project root directory.当你在项目根目录下打开 vim 时, youcompleteme会自动拾取编译数据库。 Avoid using .ycm_extra_conf.py in general and resort to using it only as last option.一般避免使用.ycm_extra_conf.py并仅将其用作最后一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM