温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ubuntu14.04如何安装vim YouCompleteMe自动补全插件

发布时间:2021-10-18 15:18:12 来源:亿速云 阅读:117 作者:小新 栏目:编程语言

这篇文章主要介绍ubuntu14.04如何安装vim YouCompleteMe自动补全插件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1, 系统环境核对!

Ubuntu 14.04.5 LTS, 64位

系统及内核版本:

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:~$

环境不相符的, 就不要往下看了...

安装 系统编译工具,依赖头文件,库

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

创建软链接
root@Linux:~/cmake-3.8.0-rc2# ln -s /usr/local/bin/cmake /usr/bin/cmake

查看现在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
添加以下行内容:
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.
官方网站 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 不指定多任务,
因为到后期连接库时,多任务导致内存溢出,内核会杀死ld进程
make 单进程,虽然慢了点,但是不会导致报错
文章尾部会贴上make -j 8时, ld 占用内存情况和 ld程序的参数, 大呼!

(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  #后来报错,内存溢出
  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       "光标到上下缓冲区边距
set nobackup          "禁止生成临时文件
set nocindent         "不使用C风格缩进
set noautoindent      "不使用自动缩进
set shiftwidth=4      "自动缩进字符宽度
set ts=4              "tab键宽度
set expandtab         "将tab符转为空格
%retab!               "对于已保存的文件,执行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自动补全插件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI