史上最完整的CentOS 6平台安装YouCompleteMe教程,没有之一!
Ubuntu/Debian/Fedora比较好安装,各种软件包都比较新
CentOS6系列很难装,各种软件包版本太低了
新手不要尝试在CentOS6系上安装,会有一种挫败感!
Linux系统不是CentOS 6系列 的少年, 请不要再往下看了。
阅读本文之前,我假设你知道什么是YouCompleteMe, 什么是C/C++语法补全.
好了,没什么废话可以再继续往下说的了,撸起袖子开始搞!
======================================================
步骤:
1, 安装操作系统
2, 文件下载
3, 更新vim
4, 安装ycm 和 Vundle
5, 编译clang
6, 编译ycm_core
7, 效果图
8, 清理工作
1, 安装操作系统:
#CentOS-6.5 基于Basic Server安装http://archive.kernel.org/centos-vault/6.5/isos/i386/CentOS-6.5-i386-bin-DVD1.isohttp://archive.kernel.org/centos-vault/6.5/isos/x86_64/CentOS-6.5-x86_64-bin-DVD1.iso #我习惯尽量不使用root权限,创建普通用户[root@CentOS ~]# useradd chunli[root@CentOS ~]# passwd chunli[root@CentOS ~]# vim /etc/sudoerschunli ALL=(ALL) ALL[root@CentOS ~]# logout chunli@ubuntu ~ ssh chunli@11.11.11.4Last login: Tue Mar 7 06:58:18 2017 from chunli-pc[chunli@CentOS ~]$
2, 文件下载
[chunli@CentOS ~]$ wget --no-check-certificate https://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gz[chunli@CentOS ~]$ wget --no-check-certificate https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz[chunli@CentOS ~]$ wget ftp://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.bz22017.03.11 写博客时,最新的llvm还是3.9.1, 2017.03.14 今天来更新博客发现llvm 4.0.0已经发布了尼玛,更新真快.....[chunli@CentOS ~]$ mkdir llvm-3.9.1[chunli@CentOS ~]$ cd llvm-3.9.1[chunli@CentOS llvm-3.9.1]$ wget http://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz[chunli@CentOS llvm-3.9.1]$ wget http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz[chunli@CentOS llvm-3.9.1]$ wget http://releases.llvm.org/3.9.1/compiler-rt-3.9.1.src.tar.xz[chunli@CentOS llvm-3.9.1]$ wget http://releases.llvm.org/3.9.1/clang-tools-extra-3.9.1.src.tar.xz
3, 更新vim
Ensure that your version of Vim is at least 7.4.143 and that it has support for Python 2 or Python 3 scripting.
#查看系统的vim ,版本太低了![chunli@CentOS ~]$ vim --versionVIM - Vi IMproved 7.2 (2008 Aug 9, compiled Apr 5 2012 10:12:47)#在线升级vim[chunli@CentOS ~]$ sudo yum update vim #提示是7.4.629[chunli@CentOS ~]$ vim --version VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 21 2016 17:06:34)Included patches: 1-207, 209-629[chunli@CentOS ~]$ vim --version| grep python #查看vim对python的支持+cryptv +linebreak +python/dyn +viminfo+cscope +lispindent -python3 +vreplacevim 问题解决了!!!
4,安装ycm, vundle
Install YCM with Vundle (or Pathogen, but Vundle is a better idea).
With Vundle, this would mean adding a Plugin 'Valloric/YouCompleteMe' line to your vimrc.
这个步骤简单,一般不会有错
[chunli@CentOS ~]$ sudo yum install git[chunli@CentOS ~]$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim[chunli@CentOS ~]$ mv ~/.vimrc ~/.vimrc.back #备份默认的vim配置文件[chunli@CentOS ~]$ vim ~/.vimrc #创建新的vim配置文件,键入如下7行filetype off set rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()Plugin 'VundleVim/Vundle.vim'Plugin 'Valloric/YouCompleteMe'call vundle#end() filetype plugin indent on [chunli@CentOS ~]$ [chunli@CentOS ~]$ vim +PluginInstall +qall #出现一个vim的窗口,下载大约15分钟[chunli@CentOS ~]$ echo $?[chunli@CentOS ~]$ du -sh ~/.vim/bundle/YouCompleteMe/ #看看下载了什么221M /home/chunli/.vim/bundle/YouCompleteMe/
5 源码编译clang !
Complete this step ONLY if you care about semantic completion support for C-family languages. Otherwise it's not necessary '
Download the latest version of libclang,YCM is designed to work with libclang version 3.9 or higher.
欢迎来到极易出错的地方, 源码编译clang !
[chunli@CentOS ~]$ cd llvm-3.9.1/[chunli@CentOS llvm-3.9.1]$ ll #我已经将这4个包放到这里了-rw-r--r-- 1 chunli chunli 9.9M 3月 5 23:37 cfe-3.9.1.src.tar.xz-rw-r--r-- 1 chunli chunli 446K 3月 7 21:48 clang-tools-extra-3.9.1.src.tar.xz-rw-r--r-- 1 chunli chunli 1.4M 3月 5 23:37 compiler-rt-3.9.1.src.tar.xz-rw-r--r-- 1 chunli chunli 18M 3月 5 23:37 llvm-3.9.1.src.tar.xz[chunli@CentOS llvm-3.9.1]$ tar xf llvm-3.9.1.src.tar.xz [chunli@CentOS llvm-3.9.1]$ tar xf clang-tools-extra-3.9.1.src.tar.xz [chunli@CentOS llvm-3.9.1]$ tar xf compiler-rt-3.9.1.src.tar.xz [chunli@CentOS llvm-3.9.1]$ tar xf cfe-3.9.1.src.tar.xz [chunli@CentOS llvm-3.9.1]$ mv cfe-3.9.1.src llvm-3.9.1.src/tools/clang[chunli@CentOS llvm-3.9.1]$ mv clang-tools-extra-3.9.1.src llvm-3.9.1.src/tools/clang/tools/extra[chunli@CentOS llvm-3.9.1]$ mv compiler-rt-3.9.1.src llvm-3.9.1.src/projects/compiler-rt#开始编译clang[chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4报错1:-bash: cmake: command not found-----------------------------------------------解决报错1: 编译安装最新的cmake[chunli@CentOS ~]$ tar xf cmake-3.8.0-rc2.tar.gz [chunli@CentOS ~]$ cd cmake-3.8.0-rc2[chunli@CentOS cmake-3.8.0-rc2]$ ./bootstrapError when bootstrapping CMake:Cannot find appropriate C compiler on this system.Please specify one using environment variable CC.See cmake_bootstrap.log for compilers attempted.报错2:Cannot find appropriate C compiler on this system.----------------------------------------------- 解决报错2:安装编译器[chunli@CentOS cmake-3.8.0-rc2]$ sudo yum install gcc[chunli@CentOS cmake-3.8.0-rc2]$ sudo yum install gcc-c++继续编译cmake[chunli@CentOS cmake-3.8.0-rc2]$ ./bootstrap[chunli@CentOS cmake-3.8.0-rc2]$ echo $?[chunli@CentOS cmake-3.8.0-rc2]$ make -j 4[chunli@CentOS cmake-3.8.0-rc2]$ echo $?[chunli@CentOS cmake-3.8.0-rc2]$ sudo make install [chunli@CentOS cmake-3.8.0-rc2]$ echo $?cmake 安装完成-----------------------------------------------------#继续编译clang[chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4CMake Error at cmake/modules/CheckCompilerVersion.cmake:12 (message):Host GCC version must be at least 4.7!Call Stack (most recent call first):cmake/config-ix.cmake:14 (include)CMakeLists.txt:461 (include)报错3:Host GCC version must be at least 4.7!-----------------------------------------------------------------解决报错3:需要高版本编译器[chunli@CentOS ~]$ gcc --version #查看系统gcc版本,确实低!gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)[chunli@CentOS ~]$ tar xf gcc-6.3.0.tar.bz2 [chunli@CentOS ~]$ cd gcc-6.3.0[chunli@CentOS gcc-6.3.0]$ ./contrib/download_prerequisites #约10分钟,自动下载依赖包[chunli@CentOS gcc-6.3.0]$ echo $?[chunli@CentOS gcc-6.3.0]$ ./configure --enable-checking=release --enable-languages=c,c++ --disable-multilib && make -j 4 #约1小时[chunli@CentOS gcc-6.3.0]$ echo $? #查看编译是否完成[chunli@CentOS gcc-6.3.0]$ sudo yum remove gcc #确认移除自带的编译器 [chunli@CentOS gcc-6.3.0]$ sudo yum remove g++ #确认移除自带的编译器[chunli@CentOS gcc-6.3.0]$ sudo yum remove gcc-c++ [chunli@CentOS gcc-6.3.0]$ gcc #检查是否真的移除了 -bash: gcc: command not found [chunli@CentOS gcc-6.3.0]$ g++-bash: g++: command not found[chunli@CentOS gcc-6.3.0]$ sudo make install #开始安装最新的编译器[chunli@CentOS gcc-6.3.0]$ echo $?[chunli@CentOS gcc-6.3.0]$ gcc --version #检查新版的编译器gcc (GCC) 6.3.0[chunli@CentOS gcc-6.3.0]$ g++ --versiong++ (GCC) 6.3.0编译器安装完成------------------------------------------------------- #继续编译clang, [chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4CMake Error at CMakeLists.txt:48 (project): The CMAKE_C_COMPILER: /usr/bin/ccCMake Error at CMakeLists.txt:48 (project): The CMAKE_CXX_COMPILER: /usr/bin/c++CMake Error at CMakeLists.txt:48 (project): The CMAKE_ASM_COMPILER: /usr/bin/cc 然而又报错啦4:找不到编译器 -----------------------------------报错4, 解决方案[chunli@CentOS llvm-3.9.1]$ which gcc/usr/local/bin/gcc[chunli@CentOS llvm-3.9.1]$ which g++/usr/local/bin/g++设置编译器路径,继续编译clang[chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_ASM_COMPILER=/usr/local/bin/gcc -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4----------------------------#继续编译clang, [chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_ASM_COMPILER=/usr/local/bin/gcc -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4CMake Error at CMakeLists.txt:488 (message):Python 2.7 or newer is required报错5:需求新版的python------------------------------ 报错5 解决方法:编译安装python2.7[chunli@CentOS llvm-3.9.1]$ python --version #查看当前python版本Python 2.6.6[chunli@CentOS llvm-3.9.1]$ yum list | grep python | grep 2.7 #仓库没有2.7版本[chunli@CentOS ~]$ tar xf Python-2.7.13.tar.xz [chunli@CentOS ~]$ cd Python-2.7.13[chunli@CentOS Python-2.7.13]$ ./configure --prefix=$HOME/python-2.7 #安装位置随意,自己家有写权限[chunli@CentOS Python-2.7.13]$ echo $?[chunli@CentOS Python-2.7.13]$ make -j 4[chunli@CentOS Python-2.7.13]$ make install [chunli@CentOS Python-2.7.13]$ echo $?修改python路径[chunli@CentOS Python-2.7.13]$ which python/usr/bin/python[chunli@CentOS Python-2.7.13]$ ll /usr/bin/python*-rwxr-xr-x. 2 root root 6088 Nov 22 2013 /usr/bin/pythonlrwxrwxrwx. 1 root root 6 Mar 5 23:29 /usr/bin/python2 -> python-rwxr-xr-x. 2 root root 6088 Nov 22 2013 /usr/bin/python2.6[chunli@CentOS Python-2.7.13]$ sudo ln -s -f $HOME/python-2.7/bin/python2.7 /usr/bin/python[chunli@CentOS Python-2.7.13]$ python --versionPython 2.7.13新版python就这样安装好了-----------------------#继续编译clang,[chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_ASM_COMPILER=/usr/local/bin/gcc -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4../../bin/llvm-tblgen: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ../../bin/llvm-tblgen)../../bin/llvm-tblgen: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by ../../bin/llvm-tblgen)../../bin/llvm-tblgen: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ../../bin/llvm-tblgen) '报错6 ,C++库版本低!------------------------------解决报错6:手动修改旧版c++库文件[chunli@CentOS ~]$ find /usr/local/ -name 'libstdc++.so.6*'/usr/local/lib/libstdc++.so.6.0.22/usr/local/lib/libstdc++.so.6[chunli@CentOS ~]$ ll /usr/lib/libstdc++.so.6lrwxrwxrwx. 1 root root 19 Mar 11 18:23 /usr/lib/libstdc++.so.6 -> libstdc++.so.6.0.13[chunli@CentOS ~]$ sudo ln -fs /usr/local/lib/libstdc++.so.6.0.22 /usr/lib/libstdc++.so.6----------------------------------------------------------------------#继续编译clang[chunli@CentOS llvm-3.9.1]$ cmake -G"Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$HOME/clang3.9 -DCMAKE_ASM_COMPILER=/usr/local/bin/gcc -DCMAKE_C_COMPILER=/usr/local/bin/gcc -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" llvm-3.9.1.src && make -j 4[chunli@CentOS llvm-3.9.1]$ echo $?终于编译好了!
6, 编译 ycm_core
Compile the ycm_core library that YCM needs. This library is the C++ engine that YCM uses to get fast completions.
[chunli@CentOS ~]$ mkdir ycm_build[chunli@CentOS ~]$ cd ycm_build#开始编译ycm_core[chunli@CentOS ycm_build]$ cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=$HOME/llvm-3.9.1/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp报错7: 找不到python-develCMake Error at /usr/local/share/cmake-3.8/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "2.6")---------------------------------------解决报错7 安装python-dev[chunli@CentOS ycm_build]$ sudo yum install python-devel----------------------------------报错8,发现yum不能用 ,是因为python的问题There was a problem importing one of the Python modulesrequired to run yum. The error leading to this problem was: No module named yum------------------------------------- 解决报错8 ,修改yum配置[chunli@CentOS ycm_build]$ which python/usr/bin/python[chunli@CentOS ycm_build]$ ll /usr/bin/python*lrwxrwxrwx. 1 root root 37 Mar 11 21:22 /usr/bin/python -> /home/chunli/python-2.7/bin/python2.7lrwxrwxrwx. 1 root root 6 Mar 5 23:29 /usr/bin/python2 -> python-rwxr-xr-x. 1 root root 6088 Nov 22 2013 /usr/bin/python2.6[chunli@CentOS ycm_build]$ [chunli@CentOS ycm_build]$ sudo vim /usr/bin/yum[chunli@CentOS ycm_build]$ cat /usr/bin/yum#!/usr/bin/python #删除此行#!/usr/bin/python2.6 #增加此行[chunli@CentOS ycm_build]$ sudo yum install python-develyum, python-devel解决了-----------------------------------#继续编译ycm_core [chunli@CentOS ycm_build]$ cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=$HOME/llvm-3.9.1/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp[chunli@CentOS ycm_build]$ echo $?[chunli@CentOS ycm_build]$ cmake --build . --target ycm_core --config Release[chunli@CentOS ycm_build]$ echo $?#ycm_core 完成![chunli@CentOS ycm_build]$ cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/[chunli@CentOS ycm_build]$ vim ~/.vimrc #添加两行let g:ycm_server_python_interpreter='/usr/bin/python'let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'================================================#启动vim 报错9,The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpected error while loading the YCM core library. Use the ':YcmToggleLogs' command to check the logs. :YcmToggleLogs #查看错误信息Available logfiles are: ycm_jHhxre.logycmd_50555_stderr_V6wuYO.logycmd_50555_stdout_Rm520B.logPress ENTER or type command to continue在ycm_jHhxre.log文件中全是报错:Failed to establish a new connection在ycmd_50555_stderr_V6wuYO.log文件中报错ImportError: /usr/lib/libstdc++.so.6: version 'GLIBCXX_3.4.20' not found (required by /home/chunli/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/../ycm_core.so) 文件ycmd_50555_stdout_Rm520B.log是空的==========================================解决报错9, GLIBCXX_3.4.20 not found[chunli@CentOS ~]$ find /usr/ -name 'libstdc++.so.6*'/usr/local/lib/libstdc++.so.6.0.22/usr/local/lib/libstdc++.so.6/usr/lib/libstdc++.so.6/usr/lib/libstdc++.so.6.0.13[chunli@CentOS ~]$ [chunli@CentOS ~]$ sudo ln -f -s /usr/local/lib/libstdc++.so.6.0.22 /usr/lib/libstdc++.so.6[chunli@CentOS ~]$ ll /usr/lib/libstdc++.so.6*lrwxrwxrwx. 1 root root 34 Mar 12 01:27 /usr/lib/libstdc++.so.6 -> /usr/local/lib/libstdc++.so.6.0.22-rwxr-xr-x. 1 root root 942040 May 10 2016 /usr/lib/libstdc++.so.6.0.13[chunli@CentOS ~]$ycm其他命令:YcmRestartServer:messages:YcmDiags:YcmDebugInfo
7 效果图
历经千辛万苦, ycm终于可以正常使用了!
先来几种自动补全图!!!!
结构体补全1
结构体补全2
头文件补全
语法检查
看看补全Linux系统结构体补全怎么样
将C头文件路径写入配置
[chunli@CentOS ~]$ vim ~/.vim/.ycm_extra_conf.py在flags中尾端追加2行'-isystem','/usr/include/',
1级补全
2级补全,看起来还不错
3 无法补全结构体中的函数指针成员,必须自己敲
ycm的工作模式,很明显是CS模式
没有运行vim时,没有ycm进程[chunli@CentOS ~]$ sudo ps aux| grep ycmchunli 3408 0.0 0.0 4356 744 pts/3 S+ 01:40 0:00 grep ycm
打开一个C文件,进入自动补全状态
[chunli@CentOS ~]$ sudo ps aux | grep ycm #会看到一个ycm守护进程chunli 4075 20.0 0.4 406820 19460 ? Ssl 01:48 0:00 /usr/bin/python /home/chunli/.vim/bundle/YouCompleteMe/python/ycm/../../third_party/ycmd/ycmd --port=54985 --options_file=/tmp/tmp0bDvy3 --log=info --idle_suicide_seconds=10800 --stdout=/tmp/ycmd_39292_stdout_oaYfWe.log --stderr=/tmp/ycmd_39292_stderr_VWlF6c.logchunli 4114 0.0 0.0 4356 720 pts/2 S+ 01:49 0:00 grep ycm[chunli@CentOS ~]$ sudo netstat -tunlap | grep pythontcp 0 0 127.0.0.1:49705 0.0.0.0:* LISTEN 4233/python tcp 0 0 127.0.0.1:49705 127.0.0.1:41286 ESTABLISHED 4233/python tcp 0 0 127.0.0.1:49705 127.0.0.1:41288 ESTABLISHED 4233/python tcp 0 0 127.0.0.1:49705 127.0.0.1:41287 ESTABLISHED 4233/python [chunli@CentOS ~]$
8 清理工作:
[chunli@CentOS ~]$ rm -rf cmake-3.8.0-rc2*[chunli@CentOS ~]$ rm -rf Python-2.7.13.* [chunli@CentOS ~]$ rm -rf python-2.7/[chunli@CentOS ~]$ rm -rf clang3.9/[chunli@CentOS ~]$ rm -rf llvm-3.9.1/[chunli@CentOS ~]$ rm -rf gcc-6.3.0*[chunli@CentOS ~]$ rm -rf ycm_build/
clang 编译好了之后,不用安装,仅仅为了得到libclang.so
python2.7 也是临时的,作为clang的依赖 不需要安装什么第三方库
gcc 也可以是临时的, 用来编译clang, 还可以得到libstdc++.so
奇怪的问题,我前面已经替换过这个文件/usr/bin/python,我以为清理python2.7.13后vim不能用了.
但是vim 的自动补全没有任何影响,来看看/usr/bin/python --version 又是2.6版本, 郁闷!
难道是安装python-devel时自动恢复/usr/bin/python为python2.6了 ?
[chunli@CentOS ~]$ ll /usr/bin/python*-rwxr-xr-x. 2 root root 3592 Aug 18 2016 /usr/bin/pythonlrwxrwxrwx. 1 root root 6 Mar 12 00:44 /usr/bin/python2 -> python-rwxr-xr-x. 2 root root 3592 Aug 18 2016 /usr/bin/python2.6-rwxr-xr-x. 1 root root 1418 Aug 18 2016 /usr/bin/python2.6-configlrwxrwxrwx. 1 root root 16 Mar 12 00:44 /usr/bin/python-config -> python2.6-config[chunli@CentOS ~]$
接下来看: