Mac安装YouCompleteMe插件

参考:http://valloric.github.io/YouCompleteMe/

http://blog.marchtea.com/archives/161

一,安装准备:

1,Vim版本7.3.584+编译时添加+python标识

//安装python-dev2.7mac默认已安装

命令行vim—version查看vim版本或进入vim后输入:version

查看是否支持python:echohas('python')输出1表示支持输入0则否

Mac通过brewinstallvim安装最新的7.4版本的vim:/usr/local/Cellar/vim/7.4.335/bin

在~/.bash_profile添加:exportPATH=/usr/local/Cellar/vim/7.4.335/bin:$PATH

2,安装cmake

查看whichcmake是否已安装,http://www.cmake.org下载cmake-2.6.4.tar.gz

tar-xzvfcmake-2.6.4.tar.gz

cdcmake-2.6.4

./bootstrap

make

makeinstall

3,下载clangbinary版本3.2以上版本官网http://clang.llvm.org

将clang+llvm-3.3-x86_64-apple-darwin12.tar解压到~/ycm_temp/llvm_root_dir目录

//~/ycm_temp/llvm_root_dir(withbin,lib,includeetc.rightinsidethatfolder)

4,安装vundle插件利用vundle安装YCM插件

$gitclonehttps://github.com/gmarik/Vundle.vim.git~/.vim/bundle/Vundle.vim

配置~/.vimrc参考:http://valloric.github.io/YouCompleteMe/

二,安装

1,执行vim+PluginInstall+qall让vundle把YCM插件下载到本地,网速慢等待比较久

YCM下载到~/.vim/bundle之后,开始编译YCM

-->cd~&&mkdirycm_build

-->cdycm_build

-->cmake-G"UnixMakefiles"-DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir.~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp//注意llvm_root_dir.~/

-->makeycm_support_libs

-->make

//itwillalsoplacethelibclang.[so|dylib]intheYouCompleteMe/pythonfolderforyouifyoucompiledwithclangsupport(itneedstobethereforYCMtowork).

如果没提示报错,则安装编译成功

2,配置

YCMlooksfora.ycm_extra_conf.pyfileinthedirectoryoftheopenedfileorinanydirectoryaboveitinthehierarchy(recursively);

Don'tjustcopy/pastethatfilesomewhereandexpectthingstomagicallywork;

yourprojectneedsdifferentflags.

Hint:justreplacethestringsintheflagsvariablewithcompilationflagsnecessaryforyourproject.

:YcmDiags//toseeifanyerrorsorwarningsweredetectedinyourfile.

:YcmForceCompileAndDiagnostics//recompile

:YcmDebugInfo

:messages

"letg:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'

letg:ycm_collect_identifiers_from_tags_files=1

letg:ycm_seed_identifiers_with_syntax=1

letg:ycm_confirm_extra_conf=0

letg:ycm_key_invoke_completion='<C-/>'"或'<M-/>'默认是CTRL+space

导航定义和声明

nnoremap<leader>gl:YcmCompleterGoToDeclaration<CR>

nnoremap<leader>gf:YcmCompleterGoToDefinition<CR>

nnoremap<leader>gg:YcmCompleterGoToDefinitionElseDeclaration<CR>

nnoremap<F5>:YcmForceCompileAndDiagnostics<CR>

YCM提供的跳跃功能采用了vim的jumplist,往前跳和往后跳的快捷键为Ctrl+O以及Ctrl+I.

问题:找不到STL相关定义:

解决方法:

callecho|clang-v-E-xc++-andlookatthepathsunderthe#include<...>searchstartshere:heading.

Youshouldtakethosepaths,prepend-isystemtoeachindividualpathandappendthemalltothelistofflagsinyour.ycm_extra_conf.pyfile.

将flags一些无用的-I-isystem需要清理掉.

stringstr;需要std::stringstr;才会补全。

.ycm_extra_conf.py文件一览:

flags=[]中的路径根据具体情况配置

~/根路径放置一个fallbackycm_extra_conf.py

然后在每个项目路径放置一个各自的ycm_extra_conf.py

#OTHERDEALINGSINTHESOFTWARE.

#

#Formoreinformation,pleasereferto<http://unlicense.org/>

importos

importycm_core

#Thesearethecompilationflagsthatwillbeusedincasethere'sno

#compilationdatabaseset(bydefault,oneisnotset).

#CHANGETHISLISTOFFLAGS.YES,THISISTHEDROIDYOUHAVEBEENLOOKINGFOR.

flags=[

'-Wall',

'-Wextra',

#'-Werror',

#'-Wc++98-compat',

'-Wno-long-long',

'-Wno-variadic-macros',

'-fexceptions',

'-stdlib=libc++',

#'-DNDEBUG',

#You100%doNOTneed-DUSE_CLANG_COMPLETERinyourflags;onlytheYCM

#sourcecodeneedsit.

#'-DUSE_CLANG_COMPLETER',

#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++',

'-I',

'.',

'-isystem',

'/System/Library/Frameworks/Python.framework/Headers',

'-isystem',

'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1',

'-isystem',

'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include',

'-isystem',

'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',

'-isystem',

'/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include',

'-isystem',

'/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks',

]

#Setthistotheabsolutepathtothefolder(NOTthefile!)containingthe

#compile_commands.jsonfiletousethatinsteadof'flags'.Seeherefor

#moredetails:http://clang.llvm.org/docs/JSONCompilationDatabase.html

#

#YoucangetCMaketogeneratethisfileforyoubyadding:

#set(CMAKE_EXPORT_COMPILE_COMMANDS1)

#toyourCMakeLists.txtfile.

#

#MostprojectswillNOTneedtosetthistoanything;youcanjustchangethe

#'flags'listofcompilationflags.NoticethatYCMitselfusesthatapproach.

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)

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_)

#NOTE:ThisisjustforYouCompleteMe;it'shighlylikelythatyourproject

#doesNOTneedtoremovethestdlibflag.DONOTUSETHISINYOUR

#ycm_extra_confIFYOU'RENOT100%SUREYOUNEEDIT.

#try:

#final_flags.remove('-stdlib=libc++')

#exceptValueError:

#pass

else:

relative_to=DirectoryOfThisScript()

final_flags=MakeRelativePathsInFlagsAbsolute(flags,relative_to)

return{

'flags':final_flags,

'do_cache':True

}

相关推荐