cmake-3.12.0
llvm-4.0.1
vim-81
wget -c http://releases.llvm.org/4.0.1/llvm-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/cfe-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/clang-tools-extra-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/compiler-rt-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/libcxx-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/libcxxabi-4.0.1.src.tar.xz
wget -c http://releases.llvm.org/4.0.1/libunwind-4.0.1.src.tar.xz
mv llvm-4.0.1.src llvm
mv cfe-4.0.1.src llvm/tools/clang
mv clang-tools-extra-4.0.1.src llvm/tools/clang/tools/extra
mv compiler-rt-4.0.1.src llvm/projects/compiler-rt
mv libcxx-4.0.1.src llvm/projects/libcxx
mv libcxxabi-4.0.1.src llvm/projects/libcxxabi
mv libunwind-4.0.1.src llvm/projects/libunwind
cmake -G "Unix Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCMAKE_BUILD_TYPE="Release" -DCMAKE_INSTALL_PREFIX=/opt/llvm-4.0.1 ../llvm
make -j4
make install
使用命令 make install-cxx install-cxxabi 安装 clang/clang++ 所需要的 libc++ 库
wget -c ftp://ftp.vim.org/pub/vim/unix/vim-8.1.tar.bz2 tar jxf vim-8.1.tar.bz2
cd vim-8.1
./configure --with-features=huge --enable-pythoninterp=yes --enable-cscope --enable-perlinterp=yes --enable-multibyte --with-x --enable-fail-if-missing --with-python-config-dir=/usr/lib64/python2.7/config/ --prefix=/opt/vim81
make -j4
make install
git clone --recursive https://github.com/Valloric/YouCompleteMe.git
git submodule update --init --recursive
yum install python-devel
cmake -G "Unix Makefiles" -DUSE_CLANG_COMPLETER=ON -DPATH_TO_LLVM_ROOT=/opt/llvm-4.0.1/ -DEXTERNAL_LIBCLANG_PATH=/opt/llvm-4.0.1/lib/libclang.so ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/
make ycm_core
编译支持 clang golang 自动补全
cd ~/.vim/bundle/YouCompleteMe$
./install.py --clang-completer --go-completer
至此,YouCompleteMe 安装成功
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
call vundle#end()
----------------- YCM CONFIG BEGIN -----------------
set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗口
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" "回车即选中当前项
let g:ycm_show_diagnostics_ui = 0 "关闭语法提示
let g:ycm_global_ycm_extra_conf = '/opt/vim81/share/vim/ycm_extra_conf.py'
let g:ycm_confirm_extra_conf=0 "关闭加载.ycm_extra_conf.py提示
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_collect_identifiers_from_tags_files=1 " 开启 YCM 基于标签引擎
let g:ycm_min_num_of_chars_for_completion=1 " 从第2个键入字符就开始罗列匹配项
let g:ycm_cache_omnifunc=0 " 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_seed_identifiers_with_syntax=0 " 语法关键字补全
" force recomile with syntastic
nnoremap <F5> :YcmForceCompileAndDiagnostics
let g:ycm_complete_in_comments = 0 "在注释输入中也能补全
let g:ycm_complete_in_strings = 0"在字符串输入中也能补全
let g:ycm_collect_identifiers_from_comments_and_strings = 0 "注释和字符串中的文字也会被收入补全
let g:clang_user_options='|| exit 0'
let g:ycm_keep_logfiles = 1
let g:ycm_log_level = 'debug'
"同时,YCM可以打开location-list来显示警告和错误的信息:YcmDiags
let g:ycm_error_symbol = '✗'
let g:ycm_warning_symbol = '⚠'
nmap <F6> :YcmDiags
set tags+=/usr/include/c++/4.8.5/stdcpp.tags
let OmniCpp_DefaultNamespaces = ["_GLIBCXX_STD"]
let mapleader=";"
let maplocalleader=";"
nnoremap:YcmCompleter GoToDefinitionElseDeclaration " 按 F3键 跳转到定义处
----------------- YCM CONFIG END -----------------
cd /usr/include/c++/4.8.5
ctags -R --c++-kinds=+l+x+p --fields=+iaSl --extra=+q --language-force=c++ -f stdcpp.tags
在 vim 中引入该标签文件。在 .vimrc 中增加如下内容:
set tags+=/usr/include/c++/4.8/stdcpp.tags
let OmniCpp_DefaultNamespaces = ["_GLIBCXX_STD"]
让 OmniCppComplete 成功识别标签文件中的标准库接口。C++ 标准库源码文件中使用了 _GLIBCXX_STD 名字空间,
(GNU C++ 标准库的实现是这样,如果你使用其他版本的标准库,需要自行查找对应的名字空间名称),
标签文件里面的各个标签都嵌套在该名字空间下,要让 OmniCppComplete 正确识别这些标签,
必须显式告知 OmniCppComplete 相应的名字空间名称。
将 系统和项目的头文件地址加到 flags 中
用命令查看库路径
echo | clang -v -E -x c++ -
#include "..." search starts here:
#include <...> search starts here:
/opt/llvm-4.0.1/bin/../include/c++/v1
/usr/local/include
/opt/llvm-4.0.1/bin/../lib/clang/4.0.1/include
/usr/include
End of search list.
将上面的 include 目录加入到 flags中
'-isystem',
'/opt/llvm-4.0.1/bin/../include/c++/v1',
'-isystem',
'/usr/local/include',
'-isystem',
'/opt/llvm-4.0.1/bin/../lib/clang/4.0.1/include',
'-isystem',
'/usr/include',
'-I',
'/home/user/projects/include', # 此处填写项目头文件路径
cwd = os.getcwd()
list_dir = os.walk(cwd)
for root, dirs, _ in list_dir:
for d in dirs:
flags.extend(["-I", os.path.join(root, d)])
用 vim81 打开一个cpp源码文件,输入命令 :YcmDebugInfo,查看 ycmd 正常运行,且能看到flags变量内容:
Printing YouCompleteMe debug information...
-- Client logfile: /tmp/ycm_8kggs4qn.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.7.3
-- Server has Clang support compiled in: True
-- Clang version: clang version 8.0.0 (tags/RELEASE_800/final)
-- Extra configuration file found and loaded
-- Extra configuration path: /home/lmz/workspace/cpp/heap_sort/.ycm_extra_conf.py
-- C-family completer debug information:
-- Compilation database path: None
-- Flags: ['-Wall', '-Wextra', '-Werror', '-fexceptions', '-DNDEBUG', '-std=c++11', '-x', 'c++', '-isyst
em', '/usr/include', '-isystem', '/usr/local/include', '-isystem']
-- Translation unit: /home/lmz/workspace/cpp/heap_sort/main.cc
-- Server running at: http://127.0.0.1:46455
-- Server process ID: 6016
-- Server logfiles:
-- /tmp/ycmd_46455_stdout_zntrr92q.log
-- /tmp/ycmd_46455_stderr_r04aq7x2.log
将光标移动到一个函数名上,按F3或输入命令 :YcmCompleter GoToDefinitionElseDeclaration,跳到 声明或定义处