ubuntu 安装YouCompleteMe实现vim代码补全,期间遇到cmake错误,clang错误,缺少pyconfig.h,ycmd server SHUT DOWN错误解决办法

vim 的 YouCompleteMe 插件可以实现代码提示和补全,但是安装过程中遇到了很多坑,这里记录一下,做个备忘,也希望可以给大家一点参考。

步骤如下:


1 下载

git clone --recursive git://github.com/Valloric/YouCompleteMe

如果执行该命令没报错, 就ok了. 但是中途有可能会断掉, 可以 cd 到 YouCompleteMe, 然后反复用如下命令再次进行下载:

git submodule update --init --recursive

又一回我是反复执行了很多次这个命令才下载全了的.

2 安装

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer  # 不需要sudo

成功的log如下:

-- [download 99% complete]
-- [download 100% complete]
Using libclang to provide semantic completion for C/C++/ObjC
Using external libclang: /tmp/ycm_build.CKY92s/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04/lib/libclang.so.3.8
-- Found PythonInterp: /usr/bin/python2.7 (found version "2.7.11") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/ycm_build.CKY92s
Scanning dependencies of target BoostParts
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_raw_buffer.cpp.o
[  0%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/regex_debug.cpp.o
[  1%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/w32_regex_traits.cpp.o
[  2%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/regex/src/wide_posix_api.cpp.o
...
[ 80%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/once_atomic.cpp.o
[ 82%] Building CXX object BoostParts/CMakeFiles/BoostParts.dir/libs/thread/src/pthread/thread.cpp.o
[ 83%] Linking CXX static library libBoostParts.a
[ 83%] Built target BoostParts
Scanning dependencies of target ycm_core
[ 83%] Building CXX object ycm/CMakeFiles/ycm_core.dir/PythonSupport.cpp.o
[ 84%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Result.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Utils.cpp.o
[ 85%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Documentation.cpp.o
[ 86%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnitStore.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangCompleter.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/TranslationUnit.cpp.o
[ 88%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangHelpers.cpp.o
[ 89%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/Range.cpp.o
[ 90%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/ClangUtils.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompletionData.cpp.o
[ 91%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ClangCompleter/CompilationDatabase.cpp.o
[ 92%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CandidateRepository.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierDatabase.cpp.o
[ 94%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNode.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/versioning.cpp.o
[ 95%] Building CXX object ycm/CMakeFiles/ycm_core.dir/Candidate.cpp.o
[ 96%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierUtils.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/ycm_core.cpp.o
[ 97%] Building CXX object ycm/CMakeFiles/ycm_core.dir/IdentifierCompleter.cpp.o
[ 98%] Building CXX object ycm/CMakeFiles/ycm_core.dir/LetterNodeListMap.cpp.o
[100%] Building CXX object ycm/CMakeFiles/ycm_core.dir/CustomAssert.cpp.o
[100%] Linking CXX shared library /home/wuyt/mutils/debian_lenny_cfg/vim_cfg/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so
[100%] Built target ycm_core

3 配置

.vimrc中加入:

" YouCompleteMe
set runtimepath+=~/.vim/bundle/YouCompleteMe
let g:ycm_collect_identifiers_from_tags_files = 1           " 开启 YCM 基于标签引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
let g:syntastic_ignore_files=[".*\.py$"]
let g:ycm_seed_identifiers_with_syntax = 1                  " 语法关键字补全
let g:ycm_complete_in_comments = 1
let g:ycm_confirm_extra_conf = 0
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']  " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
let g:ycm_complete_in_comments = 1                          " 在注释输入中也能补全
let g:ycm_complete_in_strings = 1                           " 在字符串输入中也能补全
let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0                           " 禁用语法检查
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" |            " 回车即选中当前项
nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>|     " 跳转到定义处
"let g:ycm_min_num_of_chars_for_completion=2                 " 从第2个键入字符就开始罗列匹配项

其中 ~/.vim/bundle/YouCompleteMe 为下载 YouCompleteMeu 的路径.

4 使用

• 默认当键入两个字母之后, 则启动补全. 可以通过该变量调整: g:ycm_min_num_of_chars_for_completion
• 也可以使用原有的来补全, ycm将其功能增强了.
• 按照本文配置, 可用跳转到定义处.
• 默认配置会选择补全内容, 本文的配置将其屏蔽了, 为了不会与snipmate等需要tab的冲突, 选择补全改为了.

以上转载自:https://www.linuxidc.com/Linux/2017-02/141088.htm


安装并没有上面那么顺利,期间遇到了如下错误

1 缺少 cmake

貌似从 git 上下载的很多代码编译都需要 cmake,因此按照如下步骤安装:
首先从 https://cmake.org/download/ 下载 cmake ,或者从附件下载:cmake-3.11.0-rc2.tarcmake-3.11.0-rc2.tarcmake-3.11.0-rc2.tar
执行以下命令安装:

tar -zxvf xx.tar.gz 
./bootstrap 
make 
make install

注意给与适当的权限。

2 执行 ./install.py --clang-completer 命令时提示缺少 clang

直接执行以下命令安装

sudo apt install clang

我试着先用了 apt get install 失败了,apt install 成功了。
然后执行:

python install.py --clang-completer --system-libclang

这里用的是 python 3.5。

3 安装过程中提示缺少 pyconfig.h

export CPLUS_INCLUDE_PATH=/usr/include/python3.5

最好去对应路径看看是否有 pyconfig.h,添加上就是了。

4 全部安装完成后,也设置了 vimrc,但是 vim 文件时提示 ycmd server shutdown

我出这个错误时,是用 python2.7 安装的,除了这个错误后,又重新用 python3.5 安装了一遍,成功了。

最终成功图如下:

阅读更多:   Linux笔记
添加新评论

icon_redface.gificon_idea.gificon_cool.gif2016kuk.gificon_mrgreen.gif2016shuai.gif2016tp.gif2016db.gif2016ch.gificon_razz.gif2016zj.gificon_sad.gificon_cry.gif2016zhh.gificon_question.gif2016jk.gif2016bs.gificon_lol.gif2016qiao.gificon_surprised.gif2016fendou.gif2016ll.gif