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

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

问题描述

我在 Windows 上使用 64 位 Vim,,希望你喜欢.

要解决您的问题,您必须使用 .clang_complete 文件.在 :h clang_complete 中阅读更多相关信息.简而言之,您可以将此文件放入任何项目的根目录中(即对于每个项目,此文件可以不同,这是非常有意义的,因为不同的项目具有不同的工具链配置).这是 MinGW-w64 工具链的示例:

-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

当您使用 Vim 编辑项目中的某个文件时,clang_complete 会向后遍历已编辑文件的所有父目录,直到遇到第一个 .clang_complete 文件以读.然后它读取所有这些标志/开关/定义并在调用 libclang 期间使用它们来完成.

YouCompleteMe

<小时>

这些天,我不再使用 clang_complete.那里有更强大的 Vim 语义自动完成插件.它是 YouCompleteMe.我强烈建议您尝试一下.对于 C 系列语言 (C/C++/Objective-C/Objective-C++) 的语义补全,它也使用 libclang.它依赖于强大的 C++ 后端,因此速度非常快.它与 syntastic(另一个必须有 Vim 插件)有很好的集成.它具有跳转到定义等的能力.

由于它是用 C++ 编写的,并通过 Python 粘在 Vim 上,因此您必须编译 C++ 后端.为了减轻痛苦,您可以从我的 下载预构建并准备使用的 YCM 插件适用于 Windows 的 Vim YouCompleteMe.我已经为 x86 和 x64 架构构建了它.本机组件称为 ycm_core.pyd.像往常一样,您选择的 Vim 构建的架构必须与 YCM 构建匹配(即 ycm_core.pyd).YCM 可以使用 Python 2(而不是 3),所以只要确保你有 Python 2 DLL(例如 python27.dll)和 Python 2 Interpreter(python.exe) 在 PATH 环境变量中.

如果你需要 LLVM/Clang,你也可以从我这里下载:适用于 Windows 的 LLVM.同样,只需确保在 PATH 环境变量(推荐)ORycm_core.pyd 旁边有 libclang.dll.再次支持 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++ - -v </开发/空

或在 Windows 命令提示符中:

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

并寻找以下摘录:

#include <...>搜索从这里开始:d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/x86_64-w64-mingw32d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backwardd:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/included:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixedd:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/包含搜索列表结束.

这会告诉您标准的所有路径,包括在编译代码期间隐式使用的 GCC、MinGW、MinGW-w64.

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

clang++ -E -x c++ - -v </开发/空

或者:

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

知道路径后,就可以愉快地将它们添加到.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++','-一世','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','-一世','D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward','-一世','D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include','-一世','D:/Toolchains/x64/MinGW-w64/4.8.1/POSIX/SEH/lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed','-一世','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:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++
 d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/x86_64-w64-mingw32
 d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++/backward
 d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include
 d:	oolchainsx64mingw-w644.8.1posixsehin../lib/gcc/x86_64-w64-mingw32/4.8.1/include-fixed
 d:	oolchainsx64mingw-w644.8.1posixsehin../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',

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

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