YouCompleteMe插件安装方法简述

一、前言

YouCompleteMe是VIM中进行C/C++ 开发的重要工具,可以极大提升linux下C/C++开发效率。

YCM需要高版本的gcc (8.0以上版本,支持C++17) 和 vim(8.0以上,支持python3.6以上)

二、编译gcc_8.3

1. 获取源码

  wget https://mirrors.ustc.edu.cn/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.xz -P ~

  tar xvf gcc-8.3.0.tar.xz -C ~

  cd ~/gcc-8.3.0

2. 运行 download_prerequisites 脚本

  ./contrib/download_prerequisites

3. 创建编译目录

  mkdir build-gcc-8.3.0

  cd build-gcc-8.3.0

4. 配置

  ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib --prefix=/home/jello/gcc (注意替换一下红色的用户名噢)

5. 编译

  make -j4

6. 安装

  make install

三、编译YCM

1.下载Vundle和YouCompleteMe插件

输入以下指令,下载Vundle

git clone GitHub - VundleVim/Vundle.vim: Vundle, the plug-in manager for Vim ~/.vim/bundle/Vundle.vim

2.下载成功后,在用户根目录下面,修改.vimrc文件,追加下面语句以便后续安装YouCompleteMe插件

set nocompatible

filetype off

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

Plugin ‘Valloric/YouCompleteMe’

call vundle#end()

filetype plugin indent on

3.编译YouCompleteMe

在编译之前下载编译工具,准备编译YouCompleteMe

yum install gcc gcc-c++ cmake python-devel

4.编译YouCompleteMe使其支持C/C++ 自动补全

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer

四、编译binutils_2.29

编译vim时出现连接python库失败,原因是系统的binutils版本低了,因此需要编译安装binutils

1. 下载

wget http://ftp.gnu.org/gnu/binutils/binutils-2.29.tar.gz ./

2. 编译安装

unzip binutils-2.29.tar.gz

cd binutils-2.29

./configure --prefix=/data/jzying/opt

make -j && make install

五、编译vim_8.2

1、下载
git clone GitHub - vim/vim: The official Vim repository

2、编译:

注意vim只能安装到/usr开头的目录内

rm -fr /usr/local/vim8

mkdir -p /usr/local/vim8

cd vim/src

./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-python3interp=yes \
--with-python3-config-dir=$(python3-config --configdir) \
--enable-perlinterp=yes \
--enable-luainterp=yes \
--enable-gui=gtk2 \
--enable-cscope \
--prefix=/usr/local'

其中:/usr/local/python3.6.8是python3的安装路径(python3要以--enable-shared参数安装,如果是python3.6.5以上,还需要指定:--with-openssl参数,下面会有原因说明)

----暂时不需要的config参数
--enable-rubyinterp
--enable-luainterp
--enable-perlinterp
--with-python3-config-dir=/opt2/python-3.6.5/lib/python3.6/config-3.6m-x86_64-linux-gnu
--with-python3-config-dir=/usr/local/python3.7.2/lib/python3.7/config-3.7m-x86_64-linux-gnu \

make && make install

tips:(如果make的时候出错,执行make distclean)
make distclean、


3、创建软连接
mv /usr/bin/vim /usr/bin/vim_bak
ln -sf /usr/local/vim8/bin/vim /usr/bin/vim

4、查看当前vim版本是否支持python3
vim --verison | grep python3

5、测试:
python3 import subprocess;print(subprocess)

安装jedi,jedi-vim和YouCompleteMe 都依赖这个模块来完成自动提示
pip install jedi

六、配套插件安装

1、CompleteParameter.vim

YCM使用中有个不方便的地方,是对于函数参数的提示不能持续的提示,再输入完函数名称后提示就会关闭,但可以通过该插件一定程度缓解这个问题

插件地址:GitHub - tenfyzhong/CompleteParameter.vim: Complete parameter after select the completion. Integration with YouCompleteMe(ycm), deoplete, neocomplete.

安装方法:下载代码后,拷贝到bundle目录,并在vimrc里面配置插件

配置参数:

inoremap <silent><expr> ( complete_parameter#pre_complete("()")
smap <c-j> <Plug>(complete_parameter#goto_next_parameter)
imap <c-j> <Plug>(complete_parameter#goto_next_parameter)
smap <c-k> <Plug>(complete_parameter#goto_previous_parameter)
imap <c-k> <Plug>(complete_parameter#goto_previous_parameter)

七、问题解决

1. 如果出现 AttributeError: 'module' object has no attribute 'FlagsForFile' 错误

需要添加一个配置文件,在这里添加一个 C family 类型文件模板

import os import ycm_core flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-ferror-limit=10000',
'-DNDEBUG',
'-std=c99',
'-xc',
'-isystem/usr/include/',
]

SOURCE_EXTENSIONS = [
'.cpp',
'.cxx',
'.cc',
'.c',
]
def FlagsForFile( filename, **kwargs ):
return {
'flags': flags,
'do_cache': True
}

将以上代码写入到这个文件~/.ycm_c-c++_conf.py

打开 .vimrc 配置文件 加入如下语句:

let g:ycm_global_ycm_extra_conf = "~/.ycm_c-c++_conf.py"

2. 未定义的符号错误:youcompleteme unavailable undefined symbol pyunicode_fromformat

原因:编译后,vim 需要用到:/usr/local/python3.6.8/lib/python3.6/lib-dynload/*.so动态库,

而这些动态库需要python主库的一些symbol,在vim里找不到。

解决方法参考:Issues · vim/vim · GitHub LDFLAGS="-rdynamic",因为vim中把python的库当做动态库来使用,而实际链接的库确实静态库,rdynamic可以将所有的第三方库符号一次性加载到符号表,避免出现符号未定义的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有啥问啥

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值