天天看點

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

文章目錄

    • 1 環境
    • 2 安裝YouCompleteMe前準備工作
      • 2.1 編譯安裝gcc 8.2.0
      • 2.2 編譯安裝vim8.1和Python3.7
    • 3 正式安裝YouCompleteMe
      • 3.1 安裝vundle插件
      • 3.2 編譯安裝LLVM+CLang+CMake
      • 3.3 編譯建構ycm_core庫

1 環境

首先檢視centos7版本資訊,我的版本是64位、7.5.1804(目前最新版本):

cat /etc/centos-release
uname -r
getconf LONF_BIT
           
CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

2 安裝YouCompleteMe前準備工作

YouCompleteMe的github位址:https://github.com/Valloric/YouCompleteMe

YouCompleteMe簡稱為YCM,YCM安裝前要仔細閱讀其github下的README.md檔案(重點看full installation guide下的安裝部分)。當然也可以直接跟着下面流程來走。說明一下,我的centos是剛安裝好的原生系統,還沒有安裝vim、gcc等,yum版本為3.4.3。

我之前都是用yum直接安裝vim和gcc,根本達不到版本要求,yum所安裝的版本太低,還是應該要手動編譯安裝才行得通。

2.1 編譯安裝gcc 8.2.0

首先先通過yum安裝gcc低版本(不安裝無法編譯安裝其他軟體包),yum安裝的gcc版本是4.8.5

yum install gcc gcc+ gcc-c++ -y
gcc -v
           

目前網上最新版本是8.2.0,可以選擇安裝比這個版本低一些的,但是由于要滿足c++11,是以安裝版本應大于4.8。

gcc編譯安裝需要依賴gmp,mpfr,mpc 這三個包,這三個包以及gcc的下載下傳位址都是:ftp://ftp.gnu.org/gnu

在下載下傳位址下可以看到,gcc最新版本是8.2.0,gmp的最新版本是6.1.2,mpfr的最新版本是4.0.1,mpc的最新版本是1.1.0

找到自己想下的版本,可以直接在網頁手動下載下傳,也可以輸入下面的指令:

wget ftp://ftp.gnu.org/gnu/gcc/gcc-8.2.0/gcc-8.2.0.tar.gz
wget ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.bz2
wget ftp://ftp.gnu.org/gnu/mpfr/mpfr-4.0.1.tar.bz2
wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
           

這三個包是有依賴關系的,要按以下順序編譯:

mv gmp-6.1.2.tar.bz2 /opt
cd /opt
tar -xjvf gmp-6.1.2.tar.bz2
cd /gmp-6.1.2
mkdir /usr/local/gmp-6.1.2
./configure   --prefix=/usr/local/gmp-6.1.2
make
make install

mv mpfr-4.0.1.tar.bz2 /opt
cd /opt
tar -xjvf mpfr-4.0.1.tar.bz2
cd mpfr-4.0.1
mkdir /usr/local/mpfr-4.0.1
./configure   --prefix=/usr/local/mpfr-4.0.1 --with-gmp=/usr/local/gmp-6.1.2 
make 
make install

mv mpc-1.1.0.tar.gz /opt
cd /opt
tar -xzvf mpc-1.1.0.tar.gz
cd mpc-1.1.0
mkdir /usr/local/mpc-1.1.0
./configure   --prefix=/usr/local/mpc-1.1.0 --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.0.1
make
make install
           

如果安裝gmp時出現了以下錯誤:checking for suitable m4… configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons)

說明沒有安裝m4這個庫,按照相同方法編譯安裝一下即可

wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.bz2
mv m4-1.4.18.tar.bz2 /opt
cd /opt
tar -xjvf m4-1.4.18.tar.bz2
cd m4-1.4.18
./configure   --prefix=/usr/local/
make
make install
           

所有的編譯完後,添加環境變量:

vi ~/.bashrc
           

在最後一行添上:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.1.0/lib:/usr/local/gmp-6.1.2/lib:/usr/local/mpfr-4.0.1/lib
           

儲存退出,輸入“source ~/.bashrc”指令,立即生效。接下來就可以編譯gcc了:

mv gcc-8.2.0.tar.gz /opt
cd /opt
tar -xzvf gcc-8.2.0.tar.gz
cd gcc-8.2.0
mkdir gcc_built
mkdir  /usr/local/gcc-8.2.0 
cd gcc_built
../configure   --prefix=/usr/local/gcc-8.2.0 --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.0.1 --with-mpc=/usr/local/mpc-1.1.0 --disable-multilib --enable--long-long --enable-threads=posix
make
make install
           

make時如果出現了以下問題,則說明沒有安裝zlib

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

zlib下載下傳位址為:http://www.zlib.net/

mv zlib-1.2.11.tar.gz /opt
cd /opt
tar -xzvf zlib-1.2.11.tar.gz
cd zlib-1.2.11.tar.gz
mkdir /usr/local/zlib-1.2.11
./configure   --prefix=/usr/local/zlib-1.2.11
make
make install
           

并把zlib添加環境變量到.bashrc中最後一行::

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/zlib-1.2.11/lib
export PATH=$PATH:/usr/local/zlib-1.2.11
           

儲存退出,輸入“source ~/.bashrc”指令,立即生效。然後再重新對gcc-8.2.0進行make&make install,編譯大概等一個小時,可以去慢慢喝杯咖啡了~

編譯完後,同樣添加環境變量到.bashrc中最後一行:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/gcc-8.2.0/lib:/usr/local/gcc-8.2.0/lib64
export PATH=$PATH:/usr/local/gcc-8.2.0/bin
           

儲存退出,輸入“source ~/.bashrc”指令,立即生效。gcc-8.2.0安裝完畢,檢視gcc版本:

gcc -v
           
CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

2.2 編譯安裝vim8.1和Python3.7

YouCompleteMe目前版本要求vim版本不低于7.4.1578,我之前就趟過很多坑,有的部落格說是不低于7.3,有的說不低于7.4,每個YCM版本要求不一樣,一定要認真看其github下的README.md要求。如下圖所示:

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

vim的github位址:https://github.com/vim/vim/releases

下載下傳最新的release版本,我這裡是vim-8.1.0436.tar.gz

先把現有的vim都解除安裝:

yum remove vim* -y
           

然後安裝中斷字元處理庫ncurses等一些必要的元件:

yum install -y ncurses-devel tcl-devel libffi-devel libXt-devel gtk2-devel bzip2-devel sqlite-devel readline-devel tk-devel openssl-devel
yum install -y ruby ruby-devel lua lua-devel
yum install -y ctags git python python-devel python-pip
yum install -y perl perl-devel perl-ExtUtils-ParseXS perl-ExtUtils-CBuilder perl-ExtUtils-Embed
           

如果不需要一些包可以不用安裝,比如,如果用不到ruby語言,就可以不用安裝ruby和ruby-devel。然後安裝python3,發現yum安裝不了,如下圖:

yum install -y python3 python3-devel
           
CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

隻能手動編譯安裝了,python3.7的官方位址為:https://www.python.org/ftp/python/3.7.0/

cd /opt
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar -xvf Python-3.7.0.tgz
cd Python-3.7.0/
mkdir /usr/local/python-3.7.0
./configure --prefix=/usr/local/python-3.7.0 --enble-optimizations
make
make install
           

安裝完畢,建立python3的軟連接配接:

ln -s /usr/local/python-3.7.0/python3 /usr/bin/python3
ln -s /usr/local/python-3.7.0/pip3 /usr/bin/pip3
           

檢視python3版本:

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

好了,現在開始編譯安裝vim8.1:

mv vim-8.1.0436.tar.gz /opt/
cd /opt
tar -xzvf vim-8.1.0436.tar.gz
cd vim-8.1.0436
mkdir /usr/local/vim-8.1.0436
./configure --prefix=/usr/local/vim-8.1.0436 --with-features=huge --enable-multibyte --enable-rubyinterp=yes --enable-pythoninterp=yes --enable-python3interp=yes --enable-perlinterp=yes --enable-luainterp=yes --enable-gui=gtk2 --enable-cscope
make
make install
           

安裝完畢,添加環境變量到~/.bashrc最後一行:

/usr/local/vim-8.1.0436/bin/vim ~/.bashrc
           

添加内容為:

export PATH=$PATH:/usr/local/vim-8.1.0436/bin
alias vim='/usr/local/vim-8.1.0436/bin/vim'
           

儲存退出,輸入“source ~/.bashrc”指令,立即生效。執行vim -version

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

其中最後兩行顯示: +pyhton/dyn +pyhton3/dyn,其中+代表支援,-代表不支援。說明支援python2和python3腳本,vim8.1安裝完畢。

3 正式安裝YouCompleteMe

3.1 安裝vundle插件

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
vim ~/.vimrc
           

将以下内容寫入.vimrc:

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
 " let Vundle manage YCM, required
Plugin 'Valloric/YouCompleteMe'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
           

儲存退出,再打開vim,然後輸入以下指令:

:PluginInstall
           

接下來就等待安裝了

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

指令行 輸入vim打開,發現如下錯誤,不要謊,繼續往下安裝

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

3.2 編譯安裝LLVM+CLang+CMake

根據官方要求,libclang版本要高于3.9

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

我們全部下載下傳最新版本編譯安裝。下載下傳位址為:http://releases.llvm.org/7.0.0/

從clang官網上下載下傳包含llvm、clang和libcxx等8個檔案:

cd /usr/local/
wget -c http://releases.llvm.org/7.0.0/llvm-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/cfe-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/clang-tools-extra-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/compiler-rt-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/libcxx-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/libcxxabi-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/libunwind-7.0.0.src.tar.xz
wget -c http://releases.llvm.org/7.0.0/openmp-7.0.0.src.tar.xz
           

先解壓 llvm-7.0.0.src.tar.xz

放在 llvm-7.0.0.src/tools 目錄下的元件:

解壓 cfe-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/tools/clang
           

放在 llvm-7.0.0.src/projects 目錄下的元件:

解壓 openmp-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/projects/openmp
解壓 libcxx-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/projects/libcxx
解壓 libcxxabi-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/projects/libcxxabi
解壓 libunwind-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/projects/libunwind
解壓 compiler-rt-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/projects/compiler-rt
           

clang 的子元件:

解壓 clang-tools-extra-7.0.0.src.tar.xz,重命名為 llvm-7.0.0.src/tools/clang/tools/extra
           

都解壓縮完畢後,開始安裝cmake,目前最新版本為3.13,下載下傳位址為:https://cmake.org/files/v3.13/

cd /usr/local/
weget -c https://cmake.org/files/v3.13/cmake-3.13.0-rc1-Linux-x86_64.tar.gz
tar -xzvf cmake-3.13.0-rc1-Linux-x86_64.tar.gz
           

這個檔案解壓縮即安裝完成,添加環境變量到~/.bashrc最後一行:

vim ~/.bashrc
           

添加内容為:

export PATH=$PATH:/usr/local/cmake-3.13.0-rc1-Linux-x86_64/bin
           

/usr/local/cmake-3.13.0-rc1-Linux-x86_64/為解壓縮後的路徑

儲存退出,輸入“source ~/.bashrc”指令,立即生效。運作cmake --version檢視:

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

cmake安裝完成後,就可以開始編譯clang了

在llvm-7.0.0.src同一層目錄上建立個目錄build并進入

cd /usr/local/
mkdir clang
cd llvm-7.0.0.src
mkdir build
cd build
           

然後Configure and build LLVM and Clang:

cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local/clang -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCLANG_DEFAULT_STDLIB=libc++ -DCMAKE_BUILD_TYPE=Release /usr/local/llvm-7.0.0.src
           

如果報錯:

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

找不到swig,說明沒安裝swig,在linux下安裝非常簡單。最常見的就是通過源碼安裝。源碼大家可以在http://www.swig.org/download.html上下載下傳。然後就是安裝的老三步:configure --prefix=/usr/local/swig;make;make install。參考前面zlib的安裝。然後再執行:

cmake -G "Unix Makefiles" -DSWIG_DIR=/usr/local/swig-3.0.12 -DSWIG_EXECUTABLE=/usr/local/swig-3.0.12 -DCMAKE_INSTALL_PREFIX=/usr/local/clang -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCLANG_DEFAULT_STDLIB=libc++ -DCMAKE_BUILD_TYPE=Release /usr/local/llvm-7.0.0.src
make -j 4
make install
           

安裝完畢

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

然後重新安裝YouCompleteMe

cd ~/.vim/bundle/YouCompleteMe/
vim install.sh
           

将python改為python3

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

儲存退出,執行以下指令:

/install.sh --clang-completer
           

OK,YouCompleteMe可以用了!

3.3 編譯建構ycm_core庫

下一步,我們需要編譯一個ycm_core的庫給YCM用,這樣它就可以快速語義分析産生補全或者函數變量快速跳轉了。建立一個目錄放編譯過程中産生的檔案,通過cmake來生成makefiles檔案:

mkdir ~/.ycm_build
cd ~/.ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
           

出現錯誤。沒有安裝boost

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版

到官網下載下傳最新版的boost,https://www.boost.org/users/history/version_1_68_0.html 安裝步驟:

mv boost_1_68_0.tar.gz /opt
cd /opt
tar -xzvf boost_1_68_0.tar.gz
cd boost_1_68_0
mkdir /usr/local/boost-1.68.0
./bootstrap.sh --prefix=/usr/local/boost-1.68.0
./b2 install
           

安裝完後,添加環境變量到~/.bashrc

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/boost-1.68.0/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/boost-1.68.0/lib
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/boost-1.68.0/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/boost-1.68.0/include
           

執行“source ~/.bashrc”指令,繼續建構ycm_core

cd ~/.ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_BOOST=ON -DBOOST_ROOT=/usr/local/boost-1.68.0 -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
cmake --build . --target ycm_core
           

進行相關配置:複制 .ycm_extra_conf.py 檔案;添加 vim 配置

cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
           

添加到~/.vimrc:

let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
           

大功告成

CentOS7.5安裝GCC8.2.0+VIM8.1+Python3.7.0+LLVM7.0+VIM自動填充代碼插件YouCompleteMe完整版