天天看點

ubuntu 12.04無root權限安裝YouCompleteMe安裝llvm編譯安裝python編譯vim編譯ycm注意

#文章背景:

平時工作過程, 經常使用vim來編寫和閱讀代碼, 這就需要安裝一些vim插件. 部分插件隻需要下載下傳後, 拷貝.vim目錄下面, 在.vimrc做一配置就可以了. 但有些插件則需要依賴于其它元件, 比如常用的cscope, ctags等. 最近, 看到vim的YouCompleteMe比較不錯, 就起了念頭在工作站安裝YouCompleteMe, 簡稱ycm. 但屬于自己的公司工作站上帳戶是沒有root權限的, 這就需要管理者幫助安裝軟體(老麻煩管理者也是令人崩潰的事情), 而有些軟體則由于版本号過低會導緻一些功不能工作, 最後被迫通過直接編譯源碼的方式來達到自己的目的. 在安裝ycm的過程遇到各種依賴, 花費不少的精力, 特以在此做一記錄, 友善查閱.

另外, 這篇文章适合于對linux環境比較熟悉的, 有一定開發經驗的同學. 沒有基礎的話, 還是先學習一下基礎知識吧.

#系統環境

首先需要明确一下, 工作站的環境, ubuntu 12.04 LTS. gcc 版本為4.6. 在開始之前請确認已經安裝build-essential等必要的安裝包, 這些應用或者子產品會有依賴, 如果遇到問題可以檢視相應的幫助文檔, 檢查是否安裝相應的依賴包.

#安裝ycm所需要基本條件:

  1. Vim 7.4.143 with Python 2 or Python 3 support
  2. 如果需要支援c, c++補齊, 需要clang 4.0的支援.

#安裝步驟

支援c, c++補齊的, 需要安裝clang4.0. 由于沒有root權限, 所需要子產品都需要自己下載下傳源代碼進行編譯, 并安裝到HOME目錄下面. 在這裡所需要的軟體包都安裝到了~/usr目錄下面.

##一, 檢查所依賴的軟體包

下面這些内容可以直接到llvm.org網站檢視

http://llvm.org/docs/GettingStarted.html#requirements

http://clang.llvm.org/get_started.html

  1. Requirements:

    GNU Make 3.79, 3.79.1 Makefile/build processor

    GCC >=4.8.0 C/C++ compiler1

    python >=2.7 Automated test suite2

    zlib >=1.2.3.4 Compression library3

    Additionally, your compilation host is expected to have the usual plethora of Unix

    utilities. Specifically:

    ar — archive library builder

    bzip2 — bzip2 command for distribution generation

    bunzip2 — bunzip2 command for distribution checking

    chmod — change permissions on a file

    cat — output concatenation utility

    cp — copy files

    date — print the current date/time

    echo — print to standard output

    egrep — extended regular expression search utility

    find — find files/dirs in a file system

    grep — regular expression search utility

    gzip — gzip command for distribution generation

    gunzip — gunzip command for distribution checking

    install — install directories/files

    mkdir — create a directory

    mv — move (rename) files

    ranlib — symbol table builder for archive libraries

    rm — remove (delete) files and directories

    sed — stream editor for transforming output

    sh — Bourne shell for make build scripts

    tar — tape archive for distribution generation

    test — test things in file system

    unzip — unzip command for distribution checking

    zip — zip command for distribution generation

  2. 檢查系統是否滿足相應的條件

    2.1 檢查cmake的版本号:

    運作指令: cmake --version

    應該能看到如下的輸出:

    cmake version 3.8.0

    CMake suite maintained and supported by Kitware (kitware.com/cmake).

    2.2 檢查gcc的版本号:

    運作如下的指令: gcc --version

    應該能看到如下的輸出:

    gcc (GCC) 6.3.0

    Copyright © 2016 Free Software Foundation, Inc.

    This is free software; see the source for copying conditions. There is NO

    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    2.3 檢查python版本

    由于沒有root權限, ycm需要安排future包. 而系統中預設的python是沒有安裝future的,

    是以隻能下載下傳python源碼, 手動編譯安裝到~/usr/python-2.7目錄下面.

##二 安裝make軟體包

檢查make的版本号, 如果不滿足; 則從http://ftp.gnu.org/gnu/make/下載下傳對應的源碼包, 進行下載下傳編譯.

##三 安裝gcc

由于系統中預設的gcc的版本為4.6, 我下載下傳了gcc 6.3的軟體包, 自行編譯安裝.

gcc編譯依賴gmp, mprf, mpc, isl軟體包

  1. isl編譯安裝

    可以去這裡下載下傳isl源碼包, gcc 4.9需要isl-0.12, gcc 6.3則需要isl-0.16.

    我需要編譯的是gcc 6.3, 故下載下傳的是isl-0.16

    http://isl.gforge.inria.fr/

    解壓源代碼, 進入到源代碼目錄, 執行如下的指令:

    ./configure --prefix=/home/xxx/usr/isl-0.16

    make install

  1. gmp編譯安裝:

    通路https://gmplib.org/下載下傳gmp源代碼

    解壓源代碼, 進入到源代碼目錄, 執行如下的指令:

    ./configure --prefix=/home/xxx/usr/

    make install

  1. mpfr編譯安裝:

    通路http://www.mpfr.org/下載下傳源代碼

    解壓源代碼, 進入到源代碼目錄, 執行如下的指令:

    ./configure --prefix=/home/xxx/usr/

    make install

  2. mpc編譯安裝:

    通路http://www.multiprecision.org/index.php?prog=mpc&page=download下載下傳源代碼

    解壓源代碼, 進入到源代碼目錄, 執行如下的指令:

    ./configure --prefix=/home/xxx/usr/

    make install

  1. gcc 6.3 編譯安裝

    通路https://gcc.gnu.org/下載下傳源代碼

    解壓源代碼, 進入到源代碼目錄, 執行如下的指令:

  1. ./configure --prefix=/home/xxx/usr/gcc-6.3.0 --with-gmp-=/home/xxx/usr --with-mpfr==/home/xxx/usr --with-mpc=/home/xxx/usr

    –with-isl=/home/xxx/usr/isl-0.16 LDFLAGS="-static -L/home/xxx/usr/lib -static -L/home/xxx/usr/isl-0.16/lib"

    –enable-languages=c,c++,fortran,objc,obj-c++ --enable-shared --enable-linker-build-id --with-system-zlib

    –libexecdir=/home/xxx/usr/gcc-6.3.0/lib --without-included-gettext --enable-threads=posix

    –with-gxx-include-dir=/home/xxx/usr/include/c++/6.3.0 --libdir=/home/xxx/usr/gcc-6.3.0/lib --enable-nls --with-sysroot=/

    –enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-objc-gc --disable-werror

    –with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu

  1. make install
  2. 進入/home/xxx/usr/gcc-6.3.0/bin目錄, 執行如下的指令:

    ln -s gcc cc

    ln -s cl1 /home/xxx/usr/gcc-6.3.0/lib/gcc/x86_64-linux-gnu/6.3.0/cc1

注: xxx 代表是你自己的帳戶目錄

#四 安裝cmake

從https://cmake.org/download/下載下傳cmake源代碼包

解壓軟體包, 進入軟體包目錄

執行如下的指令:

./bootstrap --prefix=~/usr/

make

make install

安裝llvm

通路http://llvm.org/, 下載下傳對應的clang 4.0的代碼包

http://releases.llvm.org/download.html#4.0.0

編譯指令請參考如下的文檔:

http://clang.llvm.org/get_started.html

http://llvm.org/docs/GettingStarted.html#requirements

  1. 下載下傳并解壓llvm4.0源碼包
  2. 下載下傳并解壓clang4.0源碼包到llvm/tools
  3. 下載下傳并解壓clang tool extra源碼包到llvm/tools/clang/tools
  4. mkdir build_llvm4.0
  5. cmake -G “Unix Makefiles” …/llvm
  6. cmake --build .
  7. cmake -DCMAKE_BUILD_TYPE=Release

    -DCMAKE_INSTALL_PREFIX=/home/liuhj7/usr/llvm -P cmake_install.cmake

編譯安裝python

ycm需要python支援ucs4, 同時需要安裝future包, 由于沒有root權限, 幫需要下載下傳源碼自行編譯安裝.

從https://www.python.org/downloads/release/python-2713/下載下傳對應的源碼包

解壓源碼後, 進行編譯:

  1. ./configure --prefix=/home/xxx/usr/python-2.7.13-ucs4 --enable-shared --enable-ipv6 --enable-unicode=ucs4 --enable-profiling --enable-optimizations --with-lto LDFLAGS="-Wl,–rpath=/home/xxx/usr/python-2.7.13-ucs4/lib-ucs4,–enable-new-dtags"
  2. make
  3. make install
  4. 下載下傳pip.py到/home/xxx/usr/python-2.7.13-ucs4/bin
  5. cd /home/xxx/usr/python-2.7.13/bin

    ./pip.py future

編譯vim

通路http://www.vim.org/git.php, 參照說明通過git下載下傳源代碼

  1. 切換到代碼目前, 運作git tag檢視版本, 執行git co tag_you_want, 切換到你需要的版本.
  2. make distclean
  3. ./configure --with-features=huge --enable-cscope --enable-fontset=yes

    – enable-pythoninterp=yes --enable-multibyte --enable-xim

    –enable-gui=auto --enable-luainterp=yes --enable-pythoninterp=yes

    –enable-rubyinterp=yes --enable-perlinterp=yes

    –enable-cscope=yes --with-x --with-compiledby=xxx:yyyy-mm-dd

    –with-python-config-dir=/home/xxx/usr/python-2.7.13-ucs4/lib/python2.7/config/

    –disable-selinux --prefix=/home/xxx/usr/vim80

  4. make
  5. make install

編譯ycm

從這裡通過git下載下傳代碼:

https://github.com/Valloric/YouCompleteMe

  1. mkdir build_ycm
  2. cd build_ycm

    cmake -G “Unix Makefiles” -DPATH_TO_LLVM_ROOT=~/usr/llvm-4.0

    -DPYTHON_EXECUTABLE:FILEPATH=/home/liuhj7/usr/python-2.7.13-ucs4

    . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp

  3. cmake --build . --target ycm_core
  4. 編譯完成之後, 修改.vimrc檔案; 配置ycm. 可以參考:

    http://valloric.github.io/YouCompleteMe/

注意

需要把生成的lib庫添加到庫搜尋目錄裡面, 通過修改.bashrc即可.同時把需要用到的程式添加到path環境變量中, 請參考:

if [ “$PATH” == “” ]; then

export PATH=$MY_PATH

else

if [

expr match "$PATH" "$MY_PATH"

-eq 0 ]; then

export PATH=$MY_PATH:$PATH

fi

fi

MY_LD_LIBRARY_PATH=/home/xxx/usr/python-2.7.13-ucs4/lib:/home/xxx/usr/lib:/home/xxx/usr/isl-0.12/lib:/home/xxx/usr/isl-0.16/lib

if [ “$LD_LIBRARY_PATH” == “” ]; then

export LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH

else

if [

expr match "$LD_LIBRARY_PATH" "$MY_LD_LIBRARY_PATH"

-eq 0 ]; then

export LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:$LD_LIBRARY_PATH

fi

fi

這個過程是在完成之後整理出來的, 可能會有錯誤. 有問題可以告訴我. 希望這篇文檔對想折騰ycm的人有幫助, 謝謝.

繼續閱讀