YouCompleteMe插件安装全攻略和问题总结

一、安装的方法

1、安全之前确保系统安装了git和vim7.4版本,可以用yum install git 和yum install vim,其他系统可以apt安装这两个工具

2、bundle安装

git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
cd ~/.vim/plugin/YouCompleteMe
./install.py --clang-completer
在~/.vimrc中添加

" Vunble
filetype off " required!
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let Vundle manage Vundle
Bundle 'gmarik/vundle'

" vim-scripts repos
Bundle 'Valloric/YouCompleteMe'

filetype plugin indent on " required!

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
执行安装 vim +BundleInstall +qall 或者 在vim中执行 :BundleInstall
3、直接安装

git clone https://github.com/Valloric/YouCompleteMe.git /usr/share/vim/vim74/plugin/YouCompleteMe
cd /usr/share/vim/vim74/plugin/YouCompleteMe
git submodule update --init --recursive
cd /usr/share/vim/vim74/plugin/YouCompleteMe
./install.py --clang-completer
执行
sed -i '$a"YouCompleteMe\nlet g:ycm_global_ycm_extra_conf="/usr/share/vim/vim74/plugin/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py"\nnmap <C-\>G :YcmCompleter GoTo<CR>' ~/.vimrc

二、安装过程中下载的Clang.so链接库GLIBC版本不对的问题

这是因为YouCompleteMe安装脚本默认从官网下载Clang.so导致的,而不是本系统编译的,所以可以从本系统编译Clang.so替换进去就可以了。
1、安装之前安装Python2.7以上和CMake,有些系统python需要源码安装,CMake直接yum install cmake安装

wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
tar xzvf Python-2.7.11.tgz
cd Python-2.7.11
./configure --prefix=/usr/local/python2.7.11
make 
make install
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python2.7.11/bin/python2.7 /usr/bin/python
2、编译CLang

注意点:1) 不能直接在源码目录编译,必须建立一个目录编译

2)编译时必须加--enable-optimized --enable-targets=host-only选项,不然会很慢

wget http://llvm.org/releases/3.8.0/llvm-3.8.0.src.tar.xz
wget http://llvm.org/releases/3.8.0/cfe-3.8.0.src.tar.xz
wget http://llvm.org/releases/3.8.0/compiler-rt-3.8.0.src.tar.xz

tar xf llvm-3.8.0.src.tar.xz
mv llvm-3.8.0.src llvm

cp compiler-rt-3.8.0.src.tar.xz ./llvm/projects
cp cfe-3.8.0.src.tar.xz ./llvm/tools


cd ./llvm/tools
tar xf cfe-3.8.0.src.tar.xz
mv cfe-3.8.0.src clang

cd ../projects
tar xf compiler-rt-3.8.0.src.tar.xz
mv compiler-rt-3.8.0.src compiler-rt

cd ../../
mkdir build
cd build
../llvm/configure --enable-optimized --enable-targets=host-only CC=gcc CXX=g++
make -j4
make install

clang --version
ls -s /usr/local/bin/clang /usr/bin/clang

拷贝Clang.so拷贝到YouCompleteMe目录

cp ./build/Release+Asserts/lib/libclang.so /usr/share/vim/vim74/plugin/YouCompleteMe/third_party/ycmd/libclang.so.3.8
三、中文GBK支持的问题
出现类似错误,则是插件解析中文GBK的错误问题

ycmd.responses.ServerError: UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 0: invalid start byte
这个问题需要修改用到转码的地方改为支持GBK,即把字符先gbk decode, 再encode成utf8
修改文件/usr/share/vim/vim74/plugin/YouCompleteMe/third_party/ycmd/ycmd/utils.py中的函数ToUnicode

# Returns a unicode type; either the new python-future str type or the real
# unicode type. The difference shouldn't matter.
def ToUnicode( value ):
  if not value:
    return str()
  if isinstance( value, str ):
    return value
  if isinstance( value, bytes ):
    # All incoming text should be utf8
    return str( value, 'utf8' )
  return str( value )
改为

def ToUnicode( value ):
  if not value:
    return str()
  if isinstance( value, unicode ):
    return value       
  if isinstance( value, str ):
    return value       
  if isinstance( value, bytes ):
    # All incoming text should be utf8
    try:
      return str( value.decode('gbk'), 'utf8' )
    except UnicodeDecodeError,e:
      pass
    return str( value, 'utf8' )
  return str( value )


四、不能联想的问题

1、注意要把.ycm_extra_conf.py放到正确的位置
按照官方文档C-family-semantic-completion中的说法

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);
也就是说.ycm_extra_conf.py要放置在被打开的源文件的当前目录或者其上级目录下,该过程一直递归直到找到该文件或者到达顶级目录,所以普通用户将该文件放在~/下就可以了


2、补全时出现的preview窗口,buffer经常留在window中无法清空,十分annoying,直接在.vimrc中禁用preview

set completeopt-=preview 

3、要在.ycm_extra_conf.py中链接需要使用的头文件目录(否则无法读取该头文件进行补全,因为找不到)。按照如下方式添加在flags数组里:
全选复制放进.ycm_extra_conf.py的flags数值里
'-I',
'/usr/local/A/include',
'-I',
'/usr/local/B/include',






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值