C-family Vim的语义自动完成插件(C / C ++完成)使用Clang(clang_complete,YouCompleteMe) [英] C-family Semantic Autocompletion Plugins for Vim (C/C++ completion) using Clang (clang_complete, YouCompleteMe)

查看:400
本文介绍了C-family Vim的语义自动完成插件(C / C ++完成)使用Clang(clang_complete,YouCompleteMe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上使用64位Vim, 。我强烈推荐你试试。对于C语言语言(C / C ++ / Objective-C / Objective-C ++)的语义完成,它还使用 libclang 。它依赖于一个强大的C ++后端,因此是令人难以置信的快。它与 syntastic (另一个必须有Vim的插件)集成。它具有跳转到定义的能力,等等​​。



由于它是用C ++编写的,并通过Python粘贴到Vim,你必须编译C ++后端。为了缓解疼痛,您可以从我的 下载预构建并准备好使用YCM插件Vim YouCompleteMe for Windows 。我已经为x86和x64架构构建它。原生组件称为 ycm_core.pyd 。像往常一样,您选择的Vim构建的架构必须匹配YCM构建(即 ycm_core.pyd )。 YCM只能使用Python 2(不是3)工作 ONLY ,所以只要确保你有Python 2 DLL(例如 python27.dll )和Python在 PATH 环境变量中的<2解释器( python.exe )。



如果您需要LLVM / Clang,也可以从我这里下载: LLVM for Windows 。再次,确保您在 PATH 环境变量中有 libclang.dll (建议)旁边的 ycm_core.pyd 。再次支持x86和x64架构,并且架构应该同时满足Vim和YCM的架构。



关于完成:


是否应该奇迹般地找到STL在哪里?


当然不是!这只是一个基于Clang前端的自动完成系统。它如何知道你当前使用哪个工具链来编译你的代码?你可以使用任何东西:Visual C ++,Borland C ++,GCC,MinGW,MinGW-w64,LLVM / Clang等。他们每个都有自己的标准库和运行时提供。因此,在每种情况下,您必须指定当前工具链用于查找标准包括的所有路径。



例如,在GCC,MinGW,MinGW -w64,可以在POSIX shell中运行以下命令:

  g ++ -E -x c ++  - / dev / null 

或在Windows命令提示符中:

  g ++ -E -x c ++  -  -v < nul 

并查找以下摘录:

  #include< ...>搜索从这里开始:
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\ ../ lib / gcc / x86_64-w64-mingw32 / 4.8 .1 / include / c ++
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\ ../ lib / gcc / x86_64-w64-mingw32 /4.8.1/include/c++/x86_64-w64-mingw32
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\ ../ lib /gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\ ../lib/gcc/x86_64-w64-mingw32/4.8.1/include
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\ ../lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed
d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin \ ../ lib / gcc / x86_64-w64-mingw32 / 4.8.1 /../../../../ x86_64-w64-mingw32 / include
搜索列表的结尾。

这告诉你所有的标准路径包括哪些GCC,MinGW,MinGW-w64使用



对于LLVM / Clang,您也可以这样做:

  clang ++ -E -x c ++  -  -v   

或:

  clang ++ -E -x c ++  -  -v < nul 

当你知道路径时,可以将它们添加到 .ycm_extra_conf .py 。例如,在我的情况下:

 ' -  I',
'D:/ Toolchains / x64 / MinGW- w64 / 4.8.1 / POSIX / SEH / lib / gcc / x86_64-w64-mingw32 /4.8.1/include/c ++',
'-I',
'D:/ Toolchains / x64 / MinGW-w64 / 4.8.1 / POSIX / SEH / lib / gcc / x86_64-w64-mingw32 / 4.8.1 / include / c ++ / x86_64-w64-mingw32',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c ++ / backward',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed',
'-I ',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/x86_64-w64-mingw32/include',


I am using 64-bit Vim on windows, this version by Haroogan:

+python27
+python33
+huge

I am trying to use clang_complete, so I took the libclang.dll library here and I set up my vimrc correctly. libclang.dll is found.

However when I open a *.cpp file I now have a message:

libclang cannot find the builtin includes this will cause slow completion code

but I have no completion at all...

I found this post with the guy who build the version of Vim that I use, but there is no clear instruction about what to do. Can anybody help here?

UPDATE

Here is what I did:

  1. Took Vim YouCompleteMe for Windows
  2. Took LLVM for Windows
  3. Took the liclang.dll from it and stuck it in YCM python directory
  4. Took .ycm_extra_conf.py and stuck it with my cpp file

It almost work (I do not have any error message), but I still get:

解决方案

clang_complete


Thanks for using Vim for Windows, hope you enjoy.

To solve your problem you have to utilize .clang_complete file. Read more about it in :h clang_complete. In brief, you can put this file into the root directory of any of your projects (i.e. for each project this file can be different, what perfectly makes sense, since different projects have different toolchain configurations). Here is the sample for MinGW-w64 toolchain:

-ID:/Toolchains/x64/MinGW-w64/4.8.1/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++
-ID:/Toolchains/x64/MinGW-w64/4.8.1/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/x86_64-w64-mingw32
-ID:/Toolchains/x64/MinGW-w64/4.8.1/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward
-ID:/Toolchains/x64/MinGW-w64/4.8.1/lib/gcc/x86_64-w64-mingw32/4.8.1/include
-ID:/Toolchains/x64/MinGW-w64/4.8.1/lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed
-ID:/Toolchains/x64/MinGW-w64/4.8.1/x86_64-w64-mingw32/include

-ID:/Libraries/x64/MinGW-w64/4.8.1/Boost/1.54.0/include

-ID:/Libraries/x64/MinGW-w64/4.8.1/Qt/4.8.5/include

-ID:/Libraries/x64/MinGW-w64/4.8.1/Eigen/3.1.3/include

"-ID:/Libraries/x64/MinGW-w64/4.8.1/Example with Spaces/0.0.1/include"

-std=c++11

-DUNICODE

When you are editing some file in the project with Vim, clang_complete traverses backward all the parent directories of the edited file until it stumbles across the first .clang_complete file to read. Then it reads all these flags/switches/definitions and uses them during the invocation of libclang for completion.

YouCompleteMe


These days, I don't use clang_complete anymore. There is more powerful semantic autocompletion plugin for Vim our there. It's YouCompleteMe. I highly recommend you try it out. For semantic completion of C-family languages (C/C++/Objective-C/Objective-C++) it uses libclang as well. It relies on a robust C++ back end, and is therefore incredibly fast. It has great integration with syntastic (another must have plugin for Vim). It has an ability to jump to definitions, and so on.

Since it's written in C++ and glued to Vim via Python, you'd have to compile the C++ back end. To ease the pain you can download prebuilt and ready to use YCM plugin from my Vim YouCompleteMe for Windows. I've built it for both x86 and x64 architectures. The native component is called ycm_core.pyd. As usual the architecture of the Vim build you chose has to match the YCM build (i.e. ycm_core.pyd). YCM can work ONLY with Python 2 (not 3), so just make sure that you have Python 2 DLL (e.g. python27.dll) and Python 2 Interpreter (python.exe) in the PATH environment variable.

If you need LLVM/Clang, you can download it from me as well: LLVM for Windows. Again, just make sure that you have libclang.dll in the PATH environment variable (recommended) OR right next to ycm_core.pyd. Once again both x86 and x64 architectures are supported, and once again the architecture should match both Vim's and YCM's ones.

Concerning completion:

Should he magically find where is the STL?

Of course not! This is just an autocompletion system based on Clang front end. How is it supposed to know which toolchain you're currently using to compile your code? You could use anything: Visual C++, Borland C++, GCC, MinGW, MinGW-w64, LLVM/Clang, etc. Each of them has their own standard library and runtime supplied. Thus, in each case you'd have to specify all the paths which your current toolchain uses to find standard includes.

For example, in case of GCC, MinGW, MinGW-w64, you can run the following in POSIX shell:

g++ -E -x c++ - -v < /dev/null

Or in Windows Command Prompt:

g++ -E -x c++ - -v < nul

And look for the following excerpt:

#include <...> search starts here:
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/x86_64-w64-mingw32
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/include
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed
 d:\toolchains\x64\mingw-w64\4.8.1\posix\seh\bin\../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/include
End of search list.

This tells you all the paths to standard includes which GCC, MinGW, MinGW-w64 use implicitly during compilation of your code.

For LLVM/Clang, you can do the same:

clang++ -E -x c++ - -v < /dev/null

Or:

clang++ -E -x c++ - -v < nul

When you know the paths, you can happily add them into .ycm_extra_conf.py. For instance, in my case:

'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/x86_64-w64-mingw32',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed',
'-I',
'D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/x86_64-w64-mingw32/include',

这篇关于C-family Vim的语义自动完成插件(C / C ++完成)使用Clang(clang_complete,YouCompleteMe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆