设为首页 收藏本站
查看: 2376|回复: 0

[经验分享] ubuntu 14.04 安装vim YouCompleteMe自动补全插件 Linux C/C++高手必备

[复制链接]

尚未签到

发表于 2018-4-30 06:19:14 | 显示全部楼层 |阅读模式
  

  

  ubuntu 14.04 安装vim YouCompleteMe自动补全插件 Linux C/C++高手必备
  

  本文非常不适合Linux新手!
  

  1, 系统环境核对!
  2, 编译安装vim 8.0
  3, 获取YouCompleteMe,Vundle 软件包
  

  -------------------------------
  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
357Mllvm-3.9.1.src
root@Linux:~# du -sh bin/
25Gbin/
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:~#  

  

  

  

  

  自动补全效果图
DSC0000.png

DSC0001.png

DSC0002.png

DSC0003.png

DSC0004.png

DSC0005.png

DSC0006.png

  

  

  

  

  

  

  

  编译clang出错
DSC0007.png

DSC0008.png

DSC0009.png

DSC00010.png

  

  

  

  

  

  

  

  ycm 与 vim 的关系
  

DSC00011.png

DSC00012.png

  

  

  

  

  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-453762-1-1.html 上篇帖子: ubuntu 源码编译安装最新的vim 8.0 下篇帖子: Ubuntu16.04 配置 国内更新源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表