您的位置:首页 > 数据库 > Redis

YouCompleteMe的安装及配置(二):补全C++、第三方库(如:boost、redis、libevent等)等

2020-08-25 16:53 1296 查看

YCM的补全功能,使用的是clangd的基于语义分析来进行补全的,如下文:

[code]Advantages of clangd over libclang include:

1、Project wide indexing: Clangd has both dynamic and static index support. The dynamic index stores up-to-date symbols coming from any files you are currently editing, whereas static index contains project-wide symbol information. This symbol information is used for code completion and code navigation. Whereas libclang is limited to the current translation unit(TU).
2、Code navigation: Clangd provides all the GoTo requests libclang provides and it improves those using the above mentioned index information to contain project-wide information rather than just the current TU.
Rename: Clangd can perform semantic rename operations on the current file, whereas libclang doesn’t support such functionality.
3、Code Completion: Clangd can perform code completions at a lower latency than libclang; also, it has information about all the symbols in your project so it can suggest items outside your current TU and also provides proper #include insertions for those items.
Signature help: Clangd provides signature help so that you can see the names and types of arguments when calling functions.
4、Format Code: Clangd provides code formatting either for the selected lines or the whole file, whereas libclang doesn’t have such functionality.
Performance: Clangd has faster reparse and code completion times compared to libclang.

In order to perform semantic analysis such as code completion, GoTo and diagnostics, YouCompleteMe uses clangd, which makes use of clang compiler, sometimes also referred to as llvm. Like any compiler, clang also requires a set of compile flags in order to parse your code. Simply put: If clang can't parse your code, YouCompleteMe can't provide semantic analysis.

上文中最后一句:Simply put: If clang can't parse your code, YouCompleteMe can't provide semantic analysis.非常重要,如果clangd不能分析你的代码,YCM就不能提供基于语议的补全

当安装了vim(+python3)+YCM+clangd时,使用vim打开工程的.h、.cpp时,clangd会根据打开的文件进行语议猜测,然后进行提示,但是,”猜测“一般是不可靠的,所以:

[code]There are 2 methods which can be used to provide compile flags to clang:

Option 1: Use a compilation database
The easiest way to get YCM to compile your code is to use a compilation database. A compilation database is usually generated by your build system (e.g. CMake) and contains the compiler invocation for each compilation unit in your project.

For information on how to generate a compilation database, see the clang documentation. In short:

If using CMake, add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON when configuring (or add set( CMAKE_EXPORT_COMPILE_COMMANDS ON ) to CMakeLists.txt) and copy or symlink the generated database to the root of your project.
If using Ninja, check out the compdb tool (-t compdb) in its docs.
If using GNU make, check out compiledb or Bear.
For other build systems, check out .ycm_extra_conf.py below.
If no .ycm_extra_conf.py is found, YouCompleteMe automatically tries to load a compilation database if there is one.

YCM looks for a file named compile_commands.json in the directory of the opened file or in any directory above it in the hierarchy (recursively); when the file is found before a local .ycm_extra_conf.py, YouCompleteMe stops searching the directories and lets clangd take over and handle the flags.

clangd支持多种生成”补全数据库“的方式,如上面提到的,基于cmake、Ninja、GNU make、当然vs也少不了;

如果使用的是cmake的构建方式,则有两种生成”补全数据库“的方式:

第一种:在工程根目录执行:cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCMAKE_BUILD_TYPE=Debug

              执行完成后,就会在工程根目录生成“compile_commands.json"文件。

              然后使用vim重新打开工程中的文件(只要你的CMakeLists.txt配置正确),就不会报找不到第三库、依赖库的错误了

第二种:在CMakeLists.txt中,增加配置:set( CMAKE_EXPORT_COMPILE_COMMANDS ON )

               即可在reload 或构建的时候,自动生成compile_commands.json文件"

 

更详细的请参见:

1、http://clangd.llvm.org/installation.html

2、https://github.com/ycm-core/YouCompleteMe#c-family-semantic-completion

如果没有使用CMake、Ninja、GNU Make,则需要使用.ycm_extra_conf.py文件配置flags了,(曾经配置过,个人不建议使用这种试,所以不作介绍了)

[code]Option 2: Provide the flags manually
If you don't have a compilation database, or aren't able to generate one, you have to tell YouCompleteMe how to compile your code some other way.

Every C-family project is different. It is not possible for YCM to guess what compiler flags to supply for your project. Fortunately, YCM provides a mechanism for you to generate the flags for a particular file with arbitrary complexity. This is achieved by requiring you to provide a Python module which implements a trivial function which, given the file name as argument, returns a list of compiler flags to use to compile that file.

YCM looks for a .ycm_extra_conf.py file in the directory of the opened file or in any directory above it in the hierarchy (recursively); when the file is found, it is loaded (only once!) as a Python module. YCM calls a Settings method in that module which should provide it with the information necessary to compile the current file. You can also provide a path to a global configuration file with the g:ycm_global_ycm_extra_conf option, which will be used as a fallback. To prevent the execution of malicious code from a file you didn't write YCM will ask you once per .ycm_extra_conf.py if it is safe to load. This can be disabled and you can white-/blacklist files. See the g:ycm_confirm_extra_conf and g:ycm_extra_conf_globlist options respectively.

This system was designed this way so that the user can perform any arbitrary sequence of operations to produce a list of compilation flags YCM should hand to Clang.

NOTE: It is highly recommended to include -x <language> flag to libclang. This is so that the correct language is detected, particularly for header files. Common values are -x c for C, -x c++ for C++, -x objc for Objective-C, and -x cuda for CUDA.

To give you an impression, if your C++ project is trivial, and your usual compilation command is: g++ -Wall -Wextra -Werror -o FILE.o FILE.cc, then the following .ycm_extra_conf.py is enough to get semantic analysis from YouCompleteMe:

def Settings( **kwargs ):
return {
'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
}
As you can see from the trivial example, YCM calls the Settings method which returns a dictionary with a single element 'flags'. This element is a list of compiler flags to pass to libclang for the current file. The absolute path of that file is accessible under the filename key of the kwargs dictionary. That's it! This is actually enough for most projects, but for complex projects it is not uncommon to integrate directly with an existing build system using the full power of the Python language.

For a more elaborate example, see ycmd's own .ycm_extra_conf.py. You should be able to use it as a starting point. Don't just copy/paste that file somewhere and expect things to magically work; your project needs different flags. Hint: just replace the strings in the flags variable with compilation flags necessary for your project. That should be enough for 99% of projects.

You could also consider using YCM-Generator to generate the ycm_extra_conf.py file.

Errors during compilation
If Clang encounters errors when compiling the header files that your file includes, then it's probably going to take a long time to get completions. When the completion menu finally appears, it's going to have a large number of unrelated completion strings (type/function names that are not actually members). This is because Clang fails to build a precompiled preamble for your file if there are any errors in the included headers and that preamble is key to getting fast completions.

Call the :YcmDiags command to see if any errors or warnings were detected in your file.

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: