这是一个漫长的安装过程,但在工作中十分实用
最基本的准备
linux系统(本机UBUNTU 14.04)
Vim 7.3以上版本(本机Vim 7.4)
git
cmake
互联网
Ubuntu系统可以通过以下命令安装 vim , git , cmake
1. sudo apt-get install vim
2. sudo apt-get install git
3. sudo apt-get install cmake
其他系统的安装方法请自行搜索
接下来安装
LLVM-Clang 3.3
Clang 标准库
安装LLVM-Clang 3.3
注意:不要跳过任何一步,除非你可以确定已经安装或者不需要。
创建目录
mkdir ~/llvm-clang
下载
点击链接,放到刚创建的llvm-clang文件夹中
- llvm-3.3源码
- clang-3.3源码
- clang-tools-extra-3.3源码
-
compiler-rt-3.3源码
解压
终端工作目录在llvm-clang/
1. tar -xvzf llvm-3.3.src.tar.gz
2. tar -xvzf compiler-rt-3.3.src.tar.gz
3. tar -xvzf clang-tools-extra-3.3.src.tar.gz
4. tar -xvzf cfe-3.3.src.tar.gz
移动
为了可以一起编译
1. mv cfe-3.3.src/ llvm-3.3.src/tools/clang/
2. mv clang-tools-extra-3.3.src/ llvm-3.3.src/tools/clang/extra/
3. mv compiler-rt-3.3.src/ llvm-3.3.src/projects/compiler-rt/
下载
LLVM、clang 及辅助库源码
1. cd ~/llvm-clang
2. svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
3. cd llvm/tools
4. svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
5. cd ../..
6. cd llvm/tools/clang/tools
7. svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
8. cd ../../../..
9. cd llvm/projects
10. svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
11. cd ..
创建编译文件夹
防止污染源码
1. cd ~/llvm-clang
2. mkdir llvm-build
3. cd llvm-build/
4. ../llvm-3.3.src/configure --enable-optimized
此种配置后,llv-clang默认安装到目录 /usr/local/ 下, 如果想改变安装目录,则加上配置: –prefix=Path 来制定
开始编译
这个过程很漫长,请耐心等待…
1. make -j4
2. sudo make install
如果以后要卸载:
1. cd ~/llvm-clang
2. sudo make uninstall
安装Clang 标准库
clang 的标准库————libc++(接口层)和 libc++abi(实现层)需要安装头文件和动态链接库(.so) *
安装 libc++
1. cd ~/llvm-clang
2. svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
3. cd libcxx/lib
4. ./buildit
复制.so文件和头文件
1. sudo cp -r ~/llvm-clang/libcxx/include/ /usr/include/c++/v1/
2. ln -s ~/llvm-clang/libcxx/lib/libc++.so.1.0 ~/llvm-clang/libcxx/lib/libc++.so.1
3. ln -s ~/llvm-clang/libcxx/lib/libc++.so.1.0 ~/llvm-clang/libcxx/lib/libc++.so
4. sudo cp ~/llvm-clang/libcxx/lib/libc++.so* /usr/lib/
复制需要管理权限,也就是输入密码…
安装 libc++abi 和 动态链接库,以及复制…
1. cd ~/llvm-clang/
2. svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
3. cd libcxxabi/lib
4. ./buildit
5
6. sudo cp -r ~/llvm-clang/libcxxabi/include/ /usr/include/c++/v1/
7. ln -s ~/llvm-clang/libcxxabi/lib/libc++abi.so.1.0 ~/llvm-clang/libcxxabi/lib/libc++abi.so.1
8. ln -s ~/llvm-clang/libcxxabi/lib/libc++abi.so.1.0 ~/llvm-clang/libcxxabi/lib/libc++abi.so
9. sudo cp ~/llvm-clang/libcxxabi/lib/libc++abi.so* /usr/lib/
至此,前期准备完毕。
开始编译安装 YCM
- 通过 Vundle 或 git 安装 YCM
- 编译
- 编辑.ycm_extra_conf.py
- 配置 .vimrc
通过 Vundle 安装 YCM
注意:此方法简单但不直观,下载过程没有及时反馈,可能让人失去耐心(难道只有我是这样的?)
本段参考–百度linux贴吧:vim智能补全插件YouCompleteMe新手完全攻略 作者:萝卜特头
**安装Vundle **
1.将Vundle安装到 ~/.vim/bundle/vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
2.编辑 .vimrc 文件
- 打开
gedit ~/.vimrc
- 在文件末尾添加
""""""""""""""""""""" Vundle
set nocompatible
filetype off
set rtp+=~/.vim/bundle/vundle
call vundel#rc()
Bundle 'gmarik/vundle'
Bundle 'Valloric/YouCompleteMe'
filetype plugin indent on
""""""""""""""""""""" Vundle
关键的是其中以Bundle开头的行,每一个Bundle代表一个vim插件,这些省略完整URL插件都是托管在 github 上的。如果想要安装新的插件,在call vundle#rc()和filetype plugin indent on
之间添加新的Bundle插件名称
即可。
3.开始下载安装
:source ~/.vimrc
:BundleInstall
如果你发现,这怎么这么久都没动静,是正常的,几十M且歪果服务器呢。
安装过程中,正在安装的插件左边会有 > 标识,安装完毕的是 +或- 。
安装结束后,会在状态栏看见Done字样。
注意:安装结束之后,打开vim会出现错误:
ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need
to compile YCM before using it. Read the docs!
通过 git 安装 YCM
本段参考– marchtea 的博客,被Vim自动补全神器–YouCompleteMe 引用。
进入目标文件夹并下载
mkdir ~/.vim/bundle/
cd ~/.vim/bundle/
git clone --recursive https://github.com/Valloric/YouCompleteMe.git
git submodule update --init --recursive
说明:
- 创建文件夹,如果已经存在,可以跳过。
- 进入文件夹
- 从 git 下载 YCM 文件
- 检查仓库(即文件夹)的完整性,不可跳过
下载过程挺久,但能看到屏幕一堆堆东西跑过去还是比较安心的呀!
编译
- 进入文件夹开始编译
cd ~/.vim/bundle/YouCompleteMe
./install.sh --clang-completer --system-libclang
- 说明:
1.进入文件夹
2.编译。--clang-completer是用于C-Family的补全,不需要可以去掉。因为系统已经安装好了clang,所以有--system-libclang
- 创建文件夹并开始编译
mkdir ~/ycm_build
cd ~/ycm_build
cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBclang=ON -DEXTERNAL_LIBCLANG_PATH=/usr/local/lib/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
make ycm_core
make ycm_support_libs
- 说明:
1.创建编译目录
2.进入目录
3.开始编译
4.在YouCompleteMe中生成libclang.so和ycm_core.so文件
5.生成第三个文件ycm_client_support.so
注意: 如果这里出错,大概到80%时说缺少头文件<clang.h>什么的,那就是前面没有认真安装好。
编辑.ycm_extra_conf.py
打开
gedit ~/.ycm_extra_conf.py
输入(如果已存在就覆盖,也可以什么都不做)
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
import os
import ycm_core
# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
flags = [
'-Wall',
'-Wextra',
#'-Werror',
#'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-stdlib=libc++',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-I',
'.',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/include',
'-isystem',
'/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1',
]
# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''
if os.path.exists( compilation_database_folder ):
database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
database = None
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
def DirectoryOfThisScript():
return os.path.dirname( os.path.abspath( __file__ ) )
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
if not working_directory:
return list( flags )
new_flags = []
make_next_absolute = False
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
for flag in flags:
new_flag = flag
if make_next_absolute:
make_next_absolute = False
if not flag.startswith( '/' ):
new_flag = os.path.join( working_directory, flag )
for path_flag in path_flags:
if flag == path_flag:
make_next_absolute = True
break
if flag.startswith( path_flag ):
path = flag[ len( path_flag ): ]
new_flag = path_flag + os.path.join( working_directory, path )
break
if new_flag:
new_flags.append( new_flag )
return new_flags
def IsHeaderFile( filename ):
extension = os.path.splitext( filename )[ 1 ]
return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
def GetCompilationInfoForFile( filename ):
# The compilation_commands.json file generated by CMake does not have entries
# for header files. So we do our best by asking the db for flags for a
# corresponding source file, if any. If one exists, the flags for that file
# should be good enough.
if IsHeaderFile( filename ):
basename = os.path.splitext( filename )[ 0 ]
for extension in SOURCE_EXTENSIONS:
replacement_file = basename + extension
if os.path.exists( replacement_file ):
compilation_info = database.GetCompilationInfoForFile(
replacement_file )
if compilation_info.compiler_flags_:
return compilation_info
return None
return database.GetCompilationInfoForFile( filename )
def FlagsForFile( filename, **kwargs ):
if database:
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
# python list, but a "list-like" StringVec object
compilation_info = GetCompilationInfoForFile( filename )
if not compilation_info:
return None
final_flags = MakeRelativePathsInFlagsAbsolute(
compilation_info.compiler_flags_,
compilation_info.compiler_working_dir_ )
# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
#try:
# final_flags.remove( '-stdlib=libc++' )
#except ValueError:
# pass
else:
relative_to = DirectoryOfThisScript()
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
return {
'flags': final_flags,
'do_cache': True
}
此配置文件摘抄自 marchtea 的博客 , 被 Vim自动补全神器–YouCompleteMe 引用。
编辑 .vimrc
打开
gedit ~/.vimrc
复制粘贴到末尾
" 自动补全配置
set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗口
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" "回车即选中当前项
"上下左右键的行为 会显示其他信息
inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"
"youcompleteme 默认tab s-tab 和自动补全冲突
"let g:ycm_key_list_select_completion=['<c-n>']
let g:ycm_key_list_select_completion = ['<Down>']
"let g:ycm_key_list_previous_completion=['<c-p>']
let g:ycm_key_list_previous_completion = ['<Up>']
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 <F5> :YcmForceCompileAndDiagnostics<CR> "force recomile with syntastic
"nnoremap <leader>lo :lopen<CR> "open locationlist
"nnoremap <leader>lc :lclose<CR> "close locationlist
inoremap <leader><leader> <C-x><C-o>
"在注释输入中也能补全
let g:ycm_complete_in_comments = 1
"在字符串输入中也能补全
let g:ycm_complete_in_strings = 1
"注释和字符串中的文字也会被收入补全
let g:ycm_collect_identifiers_from_comments_and_strings = 0
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> " 跳转到定义处
至此 VIM 的补全神器 YCM 安装完成,如果需要更加全面的配置方法,可以在 YCM 的 github主页 上找到。
主要参考
Vim自动补全插件—-YouCompleteMe安装与配置 – zhongcq
vim智能补全插件YouCompleteMe新手完全攻略 – 萝卜特头
Vim自动补全神器–YouCompleteMe – 转自 marchtea
YCM的github项目主页
转载自(Mr_Zing)博客
网友评论