千家信息网

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

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,这篇文章主要介绍ubuntu14.04如何安装vim YouCompleteMe自动补全插件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1, 系统环境核对!Ubuntu 14
千家信息网最后更新 2025年01月19日ubuntu14.04如何安装vim YouCompleteMe自动补全插件

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

1, 系统环境核对!

Ubuntu 14.04.5 LTS, 64位系统及内核版本:chunli@Linux:~$ lsb_release -aNo LSB modules are available.Distributor ID:    UbuntuDescription:    Ubuntu 14.04.5 LTSRelease:        14.04Codename:       trustychunli@Linux:~$ uname -rm4.4.0-31-generic x86_64chunli@Linux:~$

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

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

1, 安装编译工具 root@Linux:~# apt-get install build-essential2, 安装依赖头文件和库root@Linux:~# apt-get install python-dev python3-dev3, 我的cmake版本太低编译libclang需要高版本cmake 3.4.3 or higher root@Linux:# apt-get -y autoremove cmake  #卸载旧版本的cmakeroot@Linux:~# wget https://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gzroot@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 installroot@Linux:~/cmake-3.8.0-rc2# echo $?0检验cmake的安装root@Linux:~/cmake-3.8.0-rc2# cmakebash: /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 --versioncmake 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/YouCompleteMeroot@Linux:~# git clone https://github.com/VundleVim/Vundle.vim.git   ~/.vim/bundle/Vundle.vim/bundle/Vundleroot@Linux:~# vim ~/.vimrc添加以下行内容:filetype off                   set rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()Plugin 'VundleVim/Vundle.vim'Plugin 'Valloric/YouCompleteMe'call vundle#end()             filetype plugin indent on     root@Linux:~# 安装vim 插件root@Linux:~# vim +PluginInstall +qallroot@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-summaryclang       是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.xzroot@Linux:~# wget http://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xzroot@Linux:~# wget http://releases.llvm.org/3.9.1/compiler-rt-3.9.1.src.tar.xzroot@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/clangroot@Linux:~# mv compiler-rt-3.9.1.src llvm-3.9.1.src/projects/compiler-rtroot@Linux:~# mkdir binroot@Linux:~# cd bin/2, 生成Makefile文件root@Linux:~/bin# cmake -G "Unix Makefiles" ../llvm-3.9.1.srcroot@Linux:~/bin# echo $?03, 编译并安装root@Linux:~/bin# make  && make installroot@Linux:~/bin# echo $?0----------------------------[编译说明](1), 此次 make 不指定多任务,因为到后期连接库时,多任务导致内存溢出,内核会杀死ld进程make 单进程,虽然慢了点,但是不会导致报错文章尾部会贴上make -j 8时, ld 占用内存情况和 ld程序的参数, 大呼!(2), 磁盘空间必须足够大,你看看这家伙编译结束后,搞出来的包有多大!root@Linux:~# du -sh llvm-3.9.1.src357M    llvm-3.9.1.srcroot@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 --helpOVERVIEW: clang LLVM compilerUSAGE: clang-3.9 [options] 这样 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_buildroot@Linux:~$ cd ycm_buildroot@Linux:~/ycm_build# cmake -G "Unix Makefiles" \ #开启命令换行模式-DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so . \~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpproot@Linux:~/ycm_build# echo $?0root@Linux:~/ycm_build# cmake --build . --target ycm_core --config Releaseroot@Linux:~/ycm_build# echo $?0配置YouCompleteMeroot@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 ~/.vimrcset 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!               "对于已保存的文件,执行expandtabset fencs=utf-8,ucs-bom,shift-jis,GB2312,GBK,gb18030,gbk,gb2312,cp936 "支持的字符集set ignorecase        "搜索时 忽略大小写syntax on             "语法高亮set hls               "搜索高亮set bg=dark           "字体加亮set nocompatible      "去除兼容viset backspace=indent,eol,start "允许使用退格键"vim 配色相关"colorscheme corporationcolorscheme 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.vimcall vundle#begin()Plugin 'VundleVim/Vundle.vim'Plugin 'Valloric/YouCompleteMe'call vundle#end() filetype plugin indent on   "vim 设置快捷键 模式1  F2->define, F3->declar, F4->autolet g:ycm_goto_buffer_command = 'new-tab' "跳转打开新的屏幕"let g:ycm_goto_buffer_command = 'horizontal-split' "跳转打开上下分屏map  :YcmCompleter GoToDefinition           map  :YcmCompleter GoToDeclarationmap  :YcmCompleter GoToDefinitionElseDeclaration"vim 设置快捷键 模式2"let g:ycm_goto_buffer_command = 'horizontal-vsplit' "跳转打开新的分屏 :e#退出分屏"let mapleader = '\'                                 "命令模式,\df跳转到定义,\dc跳转到声明,\de任意找"nnoremap df :YcmCompleter GoToDefinition "nnoremap de :YcmCompleter GoToDeclaration"nnoremap dc :YcmCompleter GoToDefinitionElseDeclarationchunli@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 .import osimport 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=" 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=".# 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 = NoneSOURCE_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_flagsdef 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:~#

自动补全效果图

编译clang出错

ycm 与 vim 的关系

以上是"ubuntu14.04如何安装vim YouCompleteMe自动补全插件"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0