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

前端之家收集整理的这篇文章主要介绍了ubuntu 14.04 安装vim YouCompleteMe自动补全插件 Linux C/C++高手必备前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



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


本文非常不适合Linux新手!


1,系统环境核对!

2,编译安装vim 8.0

3,获取YouCompleteMe,Vundle 软件包


-------------------------------

1,系统环境核对!

Ubuntu14.04.5LTS,64位

系统及内核版本:

chunli@Linux:~$lsb_release-a
NoLSBmodulesareavailable.
DistributorID:	Ubuntu
Description:	Ubuntu14.04.5LTS
Release:	14.04
Codename:	trusty

chunli@Linux:~$uname-rm
4.4.0-31-genericx86_64
chunli@Linux:~$


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



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

1,安装编译工具
root@Linux:~#apt-getinstallbuild-essential

2,安装依赖头文件和库
root@Linux:~#apt-getinstallpython-devpython3-dev

3,我的cmake版本太低
编译libclang需要高版本cmake3.4.3orhigher
root@Linux:#apt-get-yautoremovecmake#卸载旧版本的cmake
root@Linux:~#wgethttps://cmake.org/files/v3.8/cmake-3.8.0-rc2.tar.gz
root@Linux:~#tarxfcmake-3.8.0-rc2.tar.gz
root@Linux:~#cdcmake-3.8.0-rc2/
root@Linux:~/cmake-3.8.0-rc2#./bootstrap&&make&&makeinstall
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#whichcmake
/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
cmakeversion3.8.0-rc2

这样编译工具就算完成了


编译安装vim 8.0

ubuntu 源码编译安装最新的vim 8.0


3,获取 YouCompleteMe,Vundle 软件包

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

root@Linux:~#vim~/.vimrc
添加以下行内容:
filetypeoff
setrtp+=~/.vim/bundle/Vundle.vim
callvundle#begin()
Plugin'VundleVim/Vundle.vim'
Plugin'Valloric/YouCompleteMe'
callvundle#end()
filetypepluginindenton

root@Linux:~#

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


下载编译安装 libclang 源代码

ThelibclanglibraryitprovidesisusedtopowertheYCMsemanticcompletionengineforthoselanguages.
YCMisdesignedtoworkwithlibclangversion3.9orhigher.
官方网站http://releases.llvm.org/download.html
官方文档,值得看看:http://llvm.org/docs/GettingStarted.html#getting-started-quickly-a-summary

clang是llvm项目的C,C++,ObjectiveCandObjectiveC++前端
Compiler-RT主要是为Clang和LLVM提供运行时库的支持

root@Linux:~#wgethttp://releases.llvm.org/3.9.1/llvm-3.9.1.src.tar.xz
root@Linux:~#wgethttp://releases.llvm.org/3.9.1/cfe-3.9.1.src.tar.xz
root@Linux:~#wgethttp://releases.llvm.org/3.9.1/compiler-rt-3.9.1.src.tar.xz
root@Linux:~#tarxfcfe-3.9.1.src.tar.xz
root@Linux:~#tarxfllvm-3.9.1.src.tar.xz
root@Linux:~#tarxfcompiler-rt-3.9.1.src.tar.xz
root@Linux:~#mvcfe-3.9.1.srcllvm-3.9.1.src/tools/clang
root@Linux:~#mvcompiler-rt-3.9.1.srcllvm-3.9.1.src/projects/compiler-rt
root@Linux:~#mkdirbin
root@Linux:~#cdbin/

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

3,编译并安装
root@Linux:~/bin#make&&makeinstall
root@Linux:~/bin#echo$?
0
----------------------------
[编译说明]
(1),此次make不指定多任务,因为到后期连接库时,多任务导致内存溢出,内核会杀死ld进程
make单进程,虽然慢了点,但是不会导致报错
文章尾部会贴上make-j8时,ld占用内存情况和ld程序的参数,大呼!

(2),磁盘空间必须足够大,你看看这家伙编译结束后,搞出来的包有多大!
root@Linux:~#du-shllvm-3.9.1.src
357M	llvm-3.9.1.src
root@Linux:~#du-shbin/
25G	bin/
root@Linux:~#
-----------------------------

终于编译安装完了,晒晒编译耗时
root@Linux:~/bin#exportHISTTIMEFORMAT="%F[%T]"
root@Linux:~/bin#history|grep-A8'mkdirbin'
6872017-03-04[17:16:45]mkdirbin#开始准备
6882017-03-04[17:16:59]cdbin/
6892017-03-04[17:17:31]cmake-G"UnixMakefiles"../llvm-3.9.1.src
6902017-03-04[17:18:23]echo$?
6912017-03-04[17:19:01]make-j8&&makeinstall#后来报错,内存溢出
6922017-03-04[19:38:10]make&&makeinstall#马上更改单进程
6932017-03-04[20:24:17]echo$?#此时,编译完成
6942017-03-04[20:24:56]exportHISTTIMEFORMAT="%F[%T]"
6952017-03-04[20:25:03]history

------------------------------------------
测试clang

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

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

[说明]
我是LinuxC程序员,需要使用C家族语法自动补全
使用我自定义的libclang.so
-DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so

[编译]
root@Linux:~$cd~
root@Linux:~$mkdirycm_build
root@Linux:~$cdycm_build
root@Linux:~/ycm_build#cmake-G"UnixMakefiles"\#开启命令换行模式
-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.--targetycm_core--configRelease
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#添加两行
letg:ycm_server_python_interpreter='/usr/bin/python'
letg:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'




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

chunli@Linux:~$cat~/.vimrc
setbg=dark"黑色背景
setcompleteopt=menu"关闭草稿
setnu"显示行号
setpaste"粘贴时禁止自动缩进
setscrolloff=5"光标到上下缓冲区边距
setnobackup"禁止生成临时文件
setnocindent"不使用C风格缩进
setnoautoindent"不使用自动缩进
setshiftwidth=4"自动缩进字符宽度
setts=4"tab键宽度
setexpandtab"将tab符转为空格
%retab!"对于已保存的文件,执行expandtab
setfencs=utf-8,ucs-bom,shift-jis,GB2312,GBK,gb18030,gbk,gb2312,cp936"支持的字符集
setignorecase"搜索时忽略大小写
Syntaxon"语法高亮
sethls"搜索高亮
setbg=dark"字体加亮
setnocompatible"去除兼容vi
setbackspace=indent,eol,start"允许使用退格键

"vim配色相关
"colorschemecorporation
colorschemesolarized
"colorschememolokai

"YouCompleteMe配置相关
letg:ycm_server_python_interpreter='/usr/bin/python'
letg:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
filetypeoff
setrtp+=~/.vim/bundle/Vundle.vim
callvundle#begin()
Plugin'VundleVim/Vundle.vim'
Plugin'Valloric/YouCompleteMe'
callvundle#end()
filetypepluginindenton


"vim设置快捷键模式1F2->define,F3->declar,F4->auto
letg:ycm_goto_buffer_command='new-tab'"跳转打开新的屏幕
"letg:ycm_goto_buffer_command='horizontal-split'"跳转打开上下分屏
map<F2>:YcmCompleterGoToDefinition<CR>
map<F3>:YcmCompleterGoToDeclaration<CR>
map<F4>:YcmCompleterGoToDefinitionElseDeclaration<CR>

"vim设置快捷键模式2
"letg:ycm_goto_buffer_command='horizontal-vsplit'"跳转打开新的分屏:e#退出分屏
"letmapleader='\'"命令模式,\df跳转到定义,\dc跳转到声明,\de任意找
"nnoremap<leader>df:YcmCompleterGoToDefinition<CR>
"nnoremap<leader>de:YcmCompleterGoToDeclaration<CR>
"nnoremap<leader>dc:YcmCompleterGoToDefinitionElseDeclaration<CR>

chunli@Linux:~$


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

root@Linux:~#cat~/.vim/.ycm_extra_conf.py
#Copyright(C)2014GoogleInc.
#
#Thisfileispartofycmd.
#
#ycmdisfreesoftware:youcanredistributeitand/ormodify
#itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
#theFreeSoftwareFoundation,eitherversion3oftheLicense,or
#(atyouroption)anylaterversion.
#
#ycmdisdistributedinthehopethatitwillbeuseful,#butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
#GNUGeneralPublicLicenseformoredetails.
#
#YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
#alongwithycmd.Ifnot,see<http://www.gnu.org/licenses/>.

importos
importycm_core

#Thesearethecompilationflagsthatwillbeusedincasethere'sno
#compilationdatabaseset(bydefault,oneisnotset).
#CHANGETHISLISTOFFLAGS.YES,THISISTHEDROIDYOUHAVEBEENLOOKINGFOR.
flags=[
'-Wall','-Wextra','-Werror','-fexceptions','-DNDEBUG',#THISISIMPORTANT!Withouta"-std=<something>"flag,clangwon'tknowwhich
#languagetousewhencompilingheaders.Soitwillguess.Badly.SoC++
#headerswillbecompiledasCheaders.Youdon'twantthatsoALWAYSspecify
#a"-std=<something>".
#ForaCproject,youwouldsetthistosomethinglike'c99'insteadof
#'c++11'.
'-std=c++11',#...andthesamethinggoesforthemagic-xoptionwhichspecifiesthe
#languagethatthefilestobecompiledarewrittenin.Thisismostly
#relevantforc++headers.
#ForaCproject,youwouldsetthisto'c'insteadof'c++'.
'-x','c++','-isystem','/usr/include','/usr/local/include','/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1','/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',#add_by_chunlidate:2017.03.0500:20
'-isystem','/usr/include/c++/4.8','/usr/include/c++/4.8.2','/usr/include/x86_64-linux-gnu/c++',]


#Setthistotheabsolutepathtothefolder(NOTthefile!)containingthe
#compile_commands.jsonfiletousethatinsteadof'flags'.Seeherefor
#moredetails:http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
#MostprojectswillNOTneedtosetthistoanything;youcanjustchangethe
#'flags'listofcompilationflags.
compilation_database_folder=''

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

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

defDirectoryOfThisScript():
returnos.path.dirname(os.path.abspath(__file__))


defMakeRelativePathsInFlagsAbsolute(flags,working_directory):
ifnotworking_directory:
returnlist(flags)
new_flags=[]
make_next_absolute=False
path_flags=['-isystem','-I','-iquote','--sysroot=']
forflaginflags:
new_flag=flag

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

forpath_flaginpath_flags:
ifflag==path_flag:
make_next_absolute=True
break

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

ifnew_flag:
new_flags.append(new_flag)
returnnew_flags


defIsHeaderFile(filename):
extension=os.path.splitext(filename)[1]
returnextensionin['.h','.hxx','.hpp','.hh']


defGetCompilationInfoForFile(filename):
#Thecompilation_commands.jsonfilegeneratedbyCMakedoesnothaveentries
#forheaderfiles.Sowedoourbestbyaskingthedbforflagsfora
#correspondingsourcefile,ifany.Ifoneexists,theflagsforthatfile
#shouldbegoodenough.
ifIsHeaderFile(filename):
basename=os.path.splitext(filename)[0]
forextensioninSOURCE_EXTENSIONS:
replacement_file=basename+extension
ifos.path.exists(replacement_file):
compilation_info=database.GetCompilationInfoForFile(
replacement_file)
ifcompilation_info.compiler_flags_:
returncompilation_info
returnNone
returndatabase.GetCompilationInfoForFile(filename)


#Thisistheentrypoint;thisfunctioniscalledbyycmdtoproduceflagsfor
#afile.
defFlagsForFile(filename,**kwargs):
ifdatabase:
#Bearinmindthatcompilation_info.compiler_flags_doesNOTreturna
#pythonlist,buta"list-like"StringVecobject
compilation_info=GetCompilationInfoForFile(filename)
ifnotcompilation_info:
returnNone

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






自动补全效果

wKiom1i9aJvBdft4AAB1pVFspd0848.png

wKiom1i9aJvwzVQ5AAB1VNcksRk596.png

wKioL1i9aJyB0YR5AACBlA84EGo955.png

wKiom1i9aJ3w9NEJAACcMaA7neM864.png

wKioL1i9aJ3x0EEOAACD8J2ndBs302.png

wKiom1i9aKHRD3w2AACBEGFIpRo382.png

wKioL1i9aKKwj0LUAADHVk7tES4929.png








编译clang出错

wKiom1i9aEiCO8ATAADxEPubcjk033.png

wKioL1i9aEjy2Z0tAAAnj33vh1o040.png

wKiom1i9aEqyV2iBAAHh-Kop0Eg279.png

wKioL1i9aEqyzEsEAAB_FiQYuVY747.png








ycm 与 vim 的关系


wKiom1i9Z-KS2cT-AAEf9uujaog543.png

wKioL1i9Z-PzeGymAAGJ0CiHDfE092.png

猜你在找的Ubuntu相关文章