溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

發(fā)布時間:2021-10-18 15:18:12 來源:億速云 閱讀:124 作者:小新 欄目:編程語言

這篇文章主要介紹ubuntu14.04如何安裝vim YouCompleteMe自動補全插件,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

1, 系統(tǒng)環(huán)境核對!

Ubuntu 14.04.5 LTS, 64位

系統(tǒng)及內(nèi)核版本:

chunli@Linux:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.5 LTS
Release:	14.04
Codename:	trusty

chunli@Linux:~$ uname -rm
4.4.0-31-generic x86_64
chunli@Linux:~$

環(huán)境不相符的, 就不要往下看了...

安裝 系統(tǒng)編譯工具,依賴頭文件,庫

1, 安裝編譯工具 
root@Linux:~# apt-get install build-essential

2, 安裝依賴頭文件和庫
root@Linux:~# apt-get install python-dev python3-dev

3, 我的cmake版本太低
編譯libclang需要高版本cmake 3.4.3 or higher 
root@Linux:# apt-get -y autoremove cmake  #卸載舊版本的cmake
root@Linux:~# wget https://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gz
root@Linux:~# tar xf cmake-3.8.0-rc2.tar.gz 
root@Linux:~# cd cmake-3.8.0-rc2/
root@Linux:~/cmake-3.8.0-rc2# ./bootstrap && make && make install
root@Linux:~/cmake-3.8.0-rc2# echo $?
0

檢驗cmake的安裝
root@Linux:~/cmake-3.8.0-rc2# cmake
bash: /usr/bin/cmake: 沒有那個文件或目錄

找新的cmake路徑
root@Linux:~/cmake-3.8.0-rc2# which cmake
/usr/local/bin/cmake

創(chuàng)建軟鏈接
root@Linux:~/cmake-3.8.0-rc2# ln -s /usr/local/bin/cmake /usr/bin/cmake

查看現(xiàn)在cmake的版本
root@Linux:~/cmake-3.8.0-rc2# cmake --version
cmake version 3.8.0-rc2

這樣編譯工具就算完成了

編譯安裝vim 8.0

ubuntu 源碼編譯安裝最新的vim 8.0

3, 獲取 YouCompleteMe,Vundle 軟件包 

root@Linux:~# git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
root@Linux:~# git clone https://github.com/VundleVim/Vundle.vim.git   ~/.vim/bundle/Vundle.vim/bundle/Vundle

root@Linux:~# vim ~/.vimrc
添加以下行內(nèi)容:
filetype off                   
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
call vundle#end()             
filetype plugin indent on     

root@Linux:~# 

安裝vim 插件
root@Linux:~# vim +PluginInstall +qall
root@Linux:~# echo $?
0

下載編譯安裝 libclang 源代碼

The libclang library it provides is used to power the YCM semantic completion engine for those languages. 
YCM is designed to work with libclang version 3.9 or higher.
官方網(wǎng)站 http://releases.llvm.org/download.html
官方文檔,值得看看:http://llvm.org/docs/GettingStarted.html#getting-started-quickly-a-summary

clang       是llvm項目的 C, C++, Objective C and Objective C++ 前端
Compiler-RT 主要是為Clang和LLVM提供運行時庫的支持

root@Linux:~# wget http://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz
root@Linux:~# wget http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz
root@Linux:~# wget http://releases.llvm.org/3.9.1/compiler-rt-3.9.1.src.tar.xz
root@Linux:~# tar xf cfe-3.9.1.src.tar.xz 
root@Linux:~# tar xf llvm-3.9.1.src.tar.xz 
root@Linux:~# tar xf compiler-rt-3.9.1.src.tar.xz 
root@Linux:~# mv cfe-3.9.1.src llvm-3.9.1.src/tools/clang
root@Linux:~# mv compiler-rt-3.9.1.src llvm-3.9.1.src/projects/compiler-rt
root@Linux:~# mkdir bin
root@Linux:~# cd bin/

2, 生成Makefile文件
root@Linux:~/bin# cmake -G "Unix Makefiles" ../llvm-3.9.1.src
root@Linux:~/bin# echo $?
0

3, 編譯并安裝
root@Linux:~/bin# make  && make install
root@Linux:~/bin# echo $?
0
----------------------------
[編譯說明]
(1), 此次 make 不指定多任務,
因為到后期連接庫時,多任務導致內(nèi)存溢出,內(nèi)核會殺死ld進程
make 單進程,雖然慢了點,但是不會導致報錯
文章尾部會貼上make -j 8時, ld 占用內(nèi)存情況和 ld程序的參數(shù), 大呼!

(2), 磁盤空間必須足夠大,你看看這家伙編譯結束后,搞出來的包有多大!
root@Linux:~# du -sh llvm-3.9.1.src
357M	llvm-3.9.1.src
root@Linux:~# du -sh bin/
25G	bin/
root@Linux:~# 
-----------------------------

終于編譯安裝完了,曬曬編譯耗時
root@Linux:~/bin# export HISTTIMEFORMAT="%F [%T] "
root@Linux:~/bin# history | grep -A 8 'mkdir bin'
  687  2017-03-04 [17:16:45] mkdir bin                  #開始準備
  688  2017-03-04 [17:16:59] cd bin/
  689  2017-03-04 [17:17:31] cmake -G "Unix Makefiles" ../llvm-3.9.1.src
  690  2017-03-04 [17:18:23] echo $?
  691  2017-03-04 [17:19:01] make -j 8 && make install  #后來報錯,內(nèi)存溢出
  692  2017-03-04 [19:38:10] make  && make install      #馬上更改單進程
  693  2017-03-04 [20:24:17] echo $?                    #此時,編譯完成
  694  2017-03-04 [20:24:56] export HISTTIMEFORMAT="%F [%T] "
  695  2017-03-04 [20:25:03] history

------------------------------------------
測試clang 

root@Linux:~/bin# clang --help
OVERVIEW: clang LLVM compiler

USAGE: clang-3.9 [options] <inputs>
這樣 clang 就算安裝完成了


查找libclang.so安裝路徑,后面有用!
root@Linux:/# find / -name '*libclang.so'
/usr/local/lib/libclang.so
/home/chunli/bin/lib/libclang.so

編譯,配置YouCompleteMe

[說明]
我是Linux C程序員,需要使用C家族語法自動補全
使用我自定義的libclang.so
-DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so

[編譯]  
root@Linux:~$ cd ~
root@Linux:~$ mkdir ycm_build
root@Linux:~$ cd ycm_build
root@Linux:~/ycm_build# cmake -G "Unix Makefiles" \ #開啟命令換行模式
-DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so . \
~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
root@Linux:~/ycm_build# echo $?
0

root@Linux:~/ycm_build# cmake --build . --target ycm_core --config Release
root@Linux:~/ycm_build# echo $?
0


配置YouCompleteMe
root@Linux:~/ycm_build# cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
chunli@Linux:~$ cat ~/.vimrc #添加兩行
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'

附上改進的vim配置文件,可直接替換 ~/.vimrc

chunli@Linux:~$ cat ~/.vimrc
set bg=dark           "黑色背景
set completeopt=menu  "關閉草稿
set nu                "顯示行號
set paste             "粘貼時 禁止自動縮進
set scrolloff=5       "光標到上下緩沖區(qū)邊距
set nobackup          "禁止生成臨時文件
set nocindent         "不使用C風格縮進
set noautoindent      "不使用自動縮進
set shiftwidth=4      "自動縮進字符寬度
set ts=4              "tab鍵寬度
set expandtab         "將tab符轉為空格
%retab!               "對于已保存的文件,執(zhí)行expandtab
set fencs=utf-8,ucs-bom,shift-jis,GB2312,GBK,gb18030,gbk,gb2312,cp936 "支持的字符集
set ignorecase        "搜索時 忽略大小寫
syntax on             "語法高亮
set hls               "搜索高亮
set bg=dark           "字體加亮
set nocompatible      "去除兼容vi
set backspace=indent,eol,start "允許使用退格鍵

"vim 配色相關
"colorscheme corporation
colorscheme solarized
"colorscheme molokai

"YouCompleteMe配置相關
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
filetype off      
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
call vundle#end() 
filetype plugin indent on   


"vim 設置快捷鍵 模式1  F2->define, F3->declar, F4->auto
let g:ycm_goto_buffer_command = 'new-tab' "跳轉打開新的屏幕
"let g:ycm_goto_buffer_command = 'horizontal-split' "跳轉打開上下分屏
map <F2> :YcmCompleter GoToDefinition<CR>           
map <F3> :YcmCompleter GoToDeclaration<CR>
map <F4> :YcmCompleter GoToDefinitionElseDeclaration<CR>

"vim 設置快捷鍵 模式2
"let g:ycm_goto_buffer_command = 'horizontal-vsplit' "跳轉打開新的分屏 :e#退出分屏
"let mapleader = '\'                                 "命令模式,\df跳轉到定義,\dc跳轉到聲明,\de任意找
"nnoremap <leader>df :YcmCompleter GoToDefinition<CR> 
"nnoremap <leader>de :YcmCompleter GoToDeclaration<CR>
"nnoremap <leader>dc :YcmCompleter GoToDefinitionElseDeclaration<CR>

chunli@Linux:~$

附上改進的.ycm_extra_conf.py配置文件,可直接替換 ycm_extra_conf.py

root@Linux:~# cat ~/.vim/.ycm_extra_conf.py
# Copyright (C) 2014 Google Inc.
#
# This file is part of ycmd.
#
# ycmd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ycmd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ycmd.  If not, see <http://www.gnu.org/licenses/>.

import os
import ycm_core

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',

#add_by_chunli date:2017.03.05 00:20
'-isystem',
'/usr/include',
'-isystem',
'/usr/include/c++/4.8',
'-isystem',
'/usr/include/c++/4.8.2',
'-isystem',
'/usr/include',
'/usr/include/x86_64-linux-gnu/c++',
]


# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags.
compilation_database_folder = ''

if os.path.exists( compilation_database_folder ):
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]

def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )


def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags


def IsHeaderFile( filename ):
  extension = os.path.splitext( filename )[ 1 ]
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]


def GetCompilationInfoForFile( filename ):
  # The compilation_commands.json file generated by CMake does not have entries
  # for header files. So we do our best by asking the db for flags for a
  # corresponding source file, if any. If one exists, the flags for that file
  # should be good enough.
  if IsHeaderFile( filename ):
    basename = os.path.splitext( filename )[ 0 ]
    for extension in SOURCE_EXTENSIONS:
      replacement_file = basename + extension
      if os.path.exists( replacement_file ):
        compilation_info = database.GetCompilationInfoForFile(
          replacement_file )
        if compilation_info.compiler_flags_:
          return compilation_info
    return None
  return database.GetCompilationInfoForFile( filename )


# This is the entry point; this function is called by ycmd to produce flags for
# a file.
def FlagsForFile( filename, **kwargs ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = GetCompilationInfoForFile( filename )
    if not compilation_info:
      return None

    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_flags_,
      compilation_info.compiler_working_dir_ )
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return { 'flags': final_flags }
root@Linux:~#

自動補全效果圖

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

編譯clang出錯

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ycm 與 vim 的關系

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

ubuntu14.04如何安裝vim YouCompleteMe自動補全插件

以上是“ubuntu14.04如何安裝vim YouCompleteMe自動補全插件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI