linux YouCompleteMe 安装和使用笔记

YouCompleteMe的安装是比较麻烦的,安装就浪费了我不少时间了,我是在CentOS 6.5 上安装滴

安装主页有详细介绍:

http://valloric.github.io/YouCompleteMe/ 可是一大堆英文谁看啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!

最后我还是看了

好了进行重点解说:

我是直接进入Full Installation Guide (完整安装指导):

1.Ensure that your version of Vim is at least 7.3.598 and that it has support for python2 scripting.

必须确保你的vim版本至少7.3.598 还有vim支持python2脚本!,如果安装vim高级版本,请查看连接:

http://blog.csdn.net/mengzhisuoliu/article/details/50371441

查看vim 版本:

[root@localhost ~]# vim -version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 27 2015 19:20:39)
Garbage after option argument: "-version"
More info with: "vim -h"

查看vim是否支持python

[root@localhost ~]# vim --version | grep python
+cryptv          +linebreak       +python(这里是-号就不支持,+号则支持)          +viminfo
+cscope          +lispindent      -python3         +vreplace


2.Install YCM with Vundle (or Pathogen, but Vundle is a better idea). With Vundle, this would mean adding a Plugin 'Valloric/YouCompleteMe' line to your vimrc.

用Vundle安装YCM(或者Pathogen,但是Vundle是更好的选择).使用Vundle 直接用'Valloric/YouCompleteMe' 直接安装一个插件即可!

意思是vim ~/.vimrc 中 添加 如下文本即可

set rtp+=~/.vim/bundle/vundle/
call vundle#rc() 

" Install YouCompleteMe
Bundle 'Valloric/YouCompleteMe'

然后vim 进入命令行输入:BundleInstall 需要等待好一段时间才安装完毕了哦


3.Download the latest version of libclang. Clang is an open-source compiler that can compile C/C++/Objective-C/Objective-C++. 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.6 or higher, but can in theory work with any 3.2+ version as well.

下载最新的版本libclang.Clang是开源编译器,并且支持C/C++/C对象/C++对象.这个libclang库提供各种语言的YCM语法完成引擎!YCM 使用libclang版本 3.6或者以上的版本设计的,但是理论上也可以工作在3.2+的任何版本!

那行,按照说法我们直接下载它然后编译他,网页说直接可以下载二进制文件即可,但是我还是用源码编译然后安装!

http://llvm.org/releases/download.html#3.6.2 我是下载3.6.2这个版本的

1.Clang source code

http://llvm.org/releases/3.6.2/cfe-3.6.2.src.tar.xz

2.LLVM source code

http://llvm.org/releases/3.6.2/llvm-3.6.2.src.tar.xz

3.Compiler RT source code

http://llvm.org/releases/3.6.2/compiler-rt-3.6.2.src.tar.xz

4.Clang Tools Extra

http://llvm.org/releases/3.6.2/clang-tools-extra-3.6.2.src.tar.xz

注意:

1.gcc 的版本

yum install -y gcc-c++
安装完毕后查询
[root@localhost build]# gcc -v
Using built-in specs.
Target: i686-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
升级g++,4.4.7 不能满足,升级你的GCC

http://blog.csdn.net/mengzhisuoliu/article/details/50388709

2.看看系统有没有cmake,如果没有使用yum安装cmake,以下是安装的教程路径

http://www.cnblogs.com/mophee/archive/2013/03/19/2969456.html


构造编译的文件夹目录:

mkdir clang
cd clang

#将压缩包全部移动到clang目录
mv cfe-3.6.2.src.tar ./clang
mv llvm-3.6.2.src.tar ./clang
mv compiler-rt-3.6.2.src.tar ./clang
mv clang-tools-extra-3.6.2.src.tar ./clang
#在clang目录全部解压
tar -xf cfe-3.6.2.src.tar
tar -xf llvm-3.6.2.src.tar
tar -xf compiler-rt-3.6.2.src.tar
tar -xf clang-tools-extra-3.6.2.src.tar
#移动源码到指定目录
mv cfe-3.6.2.src clang
mv clang/ llvm-3.6.2.src/tools/

mv clang-tools-extra-3.6.2.src extra
mv extra/ llvm-3.6.2.src/tools/clang/

mv compiler-rt-3.6.2.src compiler-rt
mv compiler-rt llvm-3.6.2.src/projects/

#新建一个build目录
mkdir build
cd build
#记得加上后面的选项,不然编译时间的时候会时间很长而且占很大的文件空间
../llvm-3.6.2.src/configure --enable-optimized --enable-targets=host-only
#-j4 编译使用4核cpu一起编译,加快编译速度
make -j4
#编译完成之后
make install 

[root@localhost ~]# clang -v
clang version 3.6.2 (tags/RELEASE_362/final)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.4
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.7
Found candidate GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Selected GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Candidate multilib: .;@m32
Selected multilib: .;@m32


4.clang 安装完毕了之后:

 

cd ~/.vim/bundle/YouCompleteMe
mkdir ~/build
cd ~/build
#libclang.so 这个路径,查询的时候updatedb更新locate的数据
locate libclang.so
/root/.vim/bundle/YouCompleteMe/third_party/ycmd/libclang.so
/root/clang/build/Release+Asserts/lib/libclang.so
/usr/local/lib/libclang.so
cmake -G "Unix Makefiles"  ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ -DEXTERNAL_LIBCLANG_PATH=~/clang/build/Release+Asserts/lib/libclang.so
make ycm_core
make ycm_support_libs


这里要注意的是-DEXTERNAL_LIBCLANG_PATH这个参数,用于指定libclang.so的位置。如果不指定的话,YCM将无法正常工作,总是报:The YCM shut down, crash report...之类的错误。而这个libclang.so就是我们在编译Clang的时候生成的。因此,相关路径(也即
~/clang/build/Release+Asserts/lib/libclang.so
)需要替换成自己对应的路径。


关于make。在官方文档中只使用了:make ycm_support_libs.而我在实际操作中却屡屡失败。实际上,首先要执行:make ycm_core。这样将在 /root/.vim/bundle/YouCompleteMe/third_party/ycmd/目录下自动生成两个文件(libclang.so和ycm_core.so)

那么目前为止YouCompleteMe就安装完毕了:

剩余的工作则就是配置:

cp /root/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/
vim ~/.ycm_extra_conf.py
找到如下内容进行注释:
    try:
      final_flags.remove( '-stdlib=libc++' )
    except ValueError:
      pass
改成
    #try:
    #  final_flags.remove( '-stdlib=libc++' )
    #except ValueError:
    #  pass
接着我们vim ~/.vimrc 这个文件进行配置,然后wq保存文件
set nu
set nocompatible
syntax on
set tabstop=3
set softtabstop=3
set shiftwidth=3
set autoindent
set cindent
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" Install YouCompleteMe
Bundle 'Valloric/YouCompleteMe'

" #####YouCompleteMe Configure 
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
" 自动补全配置
set completeopt=longest,menu	"让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif	"离开插入模式后自动关闭预览窗口
inoremap         pumvisible() ? "\" : "\"	"回车即选中当前项
"上下左右键的行为 会显示其他信息
inoremap       pumvisible() ? "\" : "\"
inoremap         pumvisible() ? "\" : "\"
inoremap   pumvisible() ? "\\\" : "\"
inoremap     pumvisible() ? "\\\" : "\"

"youcompleteme  默认tab  s-tab 和自动补全冲突
"let g:ycm_key_list_select_completion=['']
let g:ycm_key_list_select_completion = ['']
"let g:ycm_key_list_previous_completion=['']
let g:ycm_key_list_previous_completion = ['']
let g:ycm_confirm_extra_conf=0 "关闭加载.ycm_extra_conf.py提示

let g:ycm_collect_identifiers_from_tags_files=1	" 开启 YCM 基于标签引擎
let g:ycm_min_num_of_chars_for_completion=2	" 从第2个键入字符就开始罗列匹配项
let g:ycm_cache_omnifunc=0	" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_seed_identifiers_with_syntax=1	" 语法关键字补全
nnoremap  :YcmForceCompileAndDiagnostics	"force recomile with syntastic
"nnoremap lo :lopen	"open locationlist
"nnoremap lc :lclose	"close locationlist
inoremap  
"在注释输入中也能补全
let g:ycm_complete_in_comments = 1
"在字符串输入中也能补全
let g:ycm_complete_in_strings = 1
"注释和字符串中的文字也会被收入补全
let g:ycm_collect_identifiers_from_comments_and_strings = 0
let g:clang_user_options='|| exit 0'
nnoremap jd :YcmCompleter GoToDefinitionElseDeclaration " 跳转到定义处
" #####YouCompleteMe Configure 


最后加入包含文件路劲.ycm_extra_conf.py 是YouCompleteMe的一份配置文件,一般都需要修改和熟悉!

1.加入一些库文件,用命令查看库路径
echo | clang -v -E -x c++ -
[root@localhost ~]# echo | clang -v -E -x c++ -
clang version 3.6.2 (tags/RELEASE_362/final)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.4
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.4.7
Found candidate GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Selected GCC installation: /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1
Candidate multilib: .;@m32
Selected multilib: .;@m32
 "/usr/local/bin/clang" -cc1 -triple i386-pc-linux-gnu -E -disable-free -main-file-name - -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.20.51.0.2 -v -dwarf-column-info -resource-dir /usr/local/bin/../lib/clang/3.6.2 -internal-isystem /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1 -internal-isystem /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu -internal-isystem /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward -internal-isystem /usr/local/include -internal-isystem /usr/local/bin/../lib/clang/3.6.2/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /root -ferror-limit 19 -fmessage-length 116 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 3.6.2 based upon LLVM 3.6.2 default target i386-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here: 
这里没有显示任何东西,所以不需要包含任何路径
#include <...> search starts here:
这里就是需要包含的路径下面这些都是需要包含的路径
 /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1
 /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu
 /usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward
 /usr/local/include
 /usr/local/bin/../lib/clang/3.6.2/include
 /usr/include

End of search list.
# 1 ""
# 1 "" 1
# 1 "" 3
# 318 "" 3
# 1 "" 1
# 1 "" 2
# 1 "" 2


将以上内容复制出来,修改成如下:
 '-isystem',
 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1',
 '-isystem',
 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu',
 '-isystem',
 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward',
 '-isystem',
 '/usr/local/include',
 '-isystem',
 '/usr/local/bin/../lib/clang/3.6.2/include',
 '-isystem',
 '/usr/include',


我们需要做改动的就是flags部分。使用-isystem标志添加系统的头文件进行解析,而-I标志用于添加第三分的头文件进行解析。我的flags在最后增加如下:

格式是:

-isystem

路径

vim ~/.ycm_extra_conf 


 71 '../llvm/tools/clang/include',
 72 '-I',
 73 '.',
 74 '-I',
 75 './ClangCompleter',
 76 '-isystem',
 77 './tests/gmock/gtest',
 78 '-isystem',
 79 './tests/gmock/gtest/include',
 80 '-isystem',
 81 './tests/gmock',
 82 '-isystem',
 83 './tests/gmock/include',
 84 '-isystem',
 85 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1',
 86 '-isystem',
 87 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/i686-pc-linux-gnu',
 88 '-isystem',
 89 '/usr/local/bin/../lib/gcc/i686-pc-linux-gnu/4.8.1/../../../../include/c++/4.8.1/backward',
 90 '-isystem',
 91 '/usr/local/include',
 92 '-isystem',
 93 '/usr/local/bin/../lib/clang/3.6.2/include',
 94 '-isystem',
 95 '/usr/include',

 96 ]
 97 
 98 
 99 # Set this to the absolute path to the folder (NOT the file!) containing the
100 # compile_commands.json file to use that instead of 'flags'. See here for

101 # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html


补全 C 语言全局函数问题(vim ~/.vimrc文件修改)

默认情况下输入 ., ->, :: 之后会触发补全函数和类, 但是默认情况下是不补全全局函数的,所以 C 语言中的 printf 之类的函数就无法补全

解决办法就是手动调用补全,对应的 YCM 函数是 ycm_key_invoke_completion
将其绑定到快捷键 let g:ycm_key_invoke_completion = ''(默认是

你可能感兴趣的:(Linux)