比較麻煩,未完待續(xù)猴鲫。对人。。拂共。牺弄。。
VIM
配置:.vimrc
以及.ycm_extra_conf.py
配置
插件:個(gè)人覺得以下幾乎是必需的插件
插件列表:
- Vundle: 這個(gè)絕對(duì)是首先裝好
- nerdtree: 目錄結(jié)構(gòu)瀏覽
- YouCompleteMe: 超級(jí)強(qiáng)大的自動(dòng)補(bǔ)全宜狐,集成了具有語法檢查功能的syntactic
- tagbar: 相當(dāng)于eclipse的outline
- ctrlp: 全局搜索+狀態(tài)條
- powerline
個(gè)人更偏愛小巧輕便势告,所以有些雞肋的插件不想裝
插件地址:
- Vundle: VundleVim/Vundle.vim
- nerdtree: scrooloose/nerdtree
- YouCompleteMe: Valloric/YouCompleteMe
- tagbar: majutsushi/tagbar
- ctrlp: kien/ctrlp.vim
- powerline: powerline/powerline
安裝YouCompleteMe
其他的插件都很好裝,就是ycm不好裝抚恒。
按照步驟來咱台,一定能成功的。
如果哪里出錯(cuò)了俭驮,仔細(xì)從頭地對(duì)照教程檢查回溺;如果每一步真的都是按照步驟來,一定可以成功混萝。
安裝Vundle
略
確保前提
Ensure that your version of Vim is at least 7.4.143 and that it has support for Python 2 or Python 3 scripting.
翻譯: 確保Vim版本至少為7.4.143遗遵,并且它支持Python 2或Python 3腳本。
驗(yàn)證:
在Vim中輸入 :version 可以查看版本譬圣。
-
After you have made sure that you have Vim 7.4.143+, type the following in Vim:
echo has('python') || has('python3')
. The output should be 1. If it's 0, then get a version of Vim with Python support.翻譯: 確定Vim 7.4.143+后瓮恭,在Vim中鍵入以下命令:
echo has('python')|| has('python3')
雄坪,輸出應(yīng)為1厘熟;如果它為0屯蹦,那么去裝一個(gè)帶有Python支持的Vim版本。
安裝YouCompleteMe插件
兩種方式安裝:
-
Vundle安裝:
Plugin 'Valloric/YouCompleteMe'
如果用Vundle更新YCM绳姨,yum_support_lib庫API改變了登澜,YCM會(huì)提醒你重新編譯它。
git安裝:先用
git clone --recursive https://github.com/Valloric/YouCompleteMe.git
獲取最新的倉庫飘庄;而后使用git submodule update --init --recursive
確認(rèn)倉庫的完整性后脑蠕,開始安裝流程
Download the latest version of libclang: 下載libclang(版本>=3.9)
翻譯: 下載最新版本的libclang(3.9以上版本)
下載地址:http://llvm.org/releases/download.html
官方建議下二進(jìn)制包:(別下錯(cuò)了)
- Clang for x86_64 Ubuntu 14.04 (.sig)
- Clang for x86_64 Ubuntu 16.04 (.sig)
- Clang for Mac OS X (.sig)
編譯安裝ycm_core庫之前戲
Compile the ycm_core library that YCM needs.
This library is the C++ engine that YCM uses to get fast completions.
翻譯: 編譯YCM需要的
ycm_core
庫。 這個(gè)庫是YCM用來獲得快速完成的C ++引擎跪削。
編譯安裝ycm_core
需要cmake
和python-dev
支持谴仙。
安裝cmake
:
-
Ubuntu:
sudo apt-get install cmake
-
Mac:
brew install cmake
安裝python-dev
:
-
Ubuntu:
sudo apt-get install python-dev python3-dev
- Mac: they should already be present(Mac下,它們是現(xiàn)成的碾盐,不需要額外裝)
Here we'll assume you installed YCM with Vundle.
That means that the top-level YCM directory is in ~/.vim/bundle/YouCompleteMe
.
翻譯: 現(xiàn)在我們假設(shè)你安裝YCM與Vundle晃跺。 這意味著頂層YCM目錄在中
~/.vim/bundle/YouCompleteMe
。
正式編譯安裝ycm_core: Compile the ycm_core library that YCM needs. This library is the C++ engine that YCM uses to get fast completions.
We'll create a new folder where build files will be placed. Run the following:
翻譯: 創(chuàng)建一個(gè)新文件夾毫玖,其中將放置構(gòu)建文件掀虎。 運(yùn)行以下命令:
cd ~
mkdir ycm_build
cd ycm_build
下一步生成makefile
,這一步很重要付枫,有點(diǎn)復(fù)雜烹玉。
這一步作者的原英文介紹特別冗長,這里總結(jié)了一下阐滩,列出來二打。作者的原文附在后面。
- 如果不需要C族語言的語義支持掂榔,在
ycm_build
目錄下執(zhí)行:cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
- 如果需要C族語言的語義支持址儒,還得分幾種情況:
-
從llvm的官網(wǎng)下載了
LLVM+Clang
的二進(jìn)制包-
解壓到:
~/ycm_temp/llvm_root_dir
該目錄下有
bin, lib, include
等文件夾 然后執(zhí)行:
cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
-
如果想用系統(tǒng)的libclang:
cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBCLANG=ON . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
-
如果想用自定義的libclang:
cmake -G "Unix Makefiles" -DEXTERNAL_LIBCLANG_PATH=/path/to/libclang.so . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
/path/to/libclang.so
這部分填入你自己編譯libclang的路徑
-
至此,makefile
已生成衅疙。
我自己是按照作者建議莲趣,從llvm網(wǎng)站下載的二進(jìn)制文件,安裝的饱溢。
生成ycm_core
cmake --build . --target ycm_core --config Release
安裝完成
至此喧伞,YouCompleteMe已經(jīng)算是安裝成功!
注意:這時(shí)候绩郎,ycm_build目錄可以刪除啦潘鲫!
安裝成功后,
ycm_build
以及ycm_temp
目錄都可以刪除肋杖,不影響YouCompleteMe插件的使用溉仑。
===================================================================================================
==================================================================================================
(可跳過)附錄:作者原文中主要步驟和關(guān)鍵點(diǎn)摘抄
Now we need to generate the makefiles. If you DON'T care about semantic support for C-family languages, run the following command in the ycm_build directory:
We'll assume you downloaded a binary distribution of LLVM+Clang from llvm.org in step 3 and that you extracted the archive file to folder ~/ycm_temp/llvm_root_dir
(with bin, lib, include etc. folders right inside that folder).
NOTE: This only works with a downloaded LLVM binary package, not a custom-built LLVM! See docs below for EXTERNAL_LIBCLANG_PATH
when using a custom LLVM build.
With that in mind, run the following command in the ycm_build
directory:
cmake -G "<generator>" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
, where <generator> is Unix Makefiles
on Unix systems.
Now that configuration files have been generated, compile the libraries using this command: cmake --build . --target ycm_core --config Release
The
--config Release
part is specific to Windows and will be ignored on a Unix OS.
For those who want to use the system version of libclang, you would pass -DUSE_SYSTEM_LIBCLANG=ON
to cmake instead of the -DPATH_TO_LLVM_ROOT=...
flag.
注意作者這里的NOTE提示:
NOTE: We STRONGLY recommend AGAINST use of the system libclang instead of the upstream compiled binaries. Random things may break. Save yourself the hassle and use the upstream pre-built libclang.
如果是custom libclang而不是downloaded LLVM binary package:
You could also force the use of a custom libclang library with -DEXTERNAL_LIBCLANG_PATH=/path/to/libclang.so
flag (the library would end with .dylib on a Mac). Again, this flag would be used instead of the other flags.
If you compiled LLVM from source, this is the flag you should be using.
Running the cmake command will also place the libclang.[so|dylib|dll]
in the YouCompleteMe/third_party/ycmd
folder for you if you compiled with clang support (it needs to be there for YCM to work).
Don't forget that if you want the C-family semantic completion engine to work,
you will need to provide the compilation flags for your project to YCM.
==================================================================================================
==================================================================================================
配置
-
前戲準(zhǔn)備
cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/ vim ~/.ycm_extra_conf.py /* ** 注:下面需要注釋的內(nèi)容只有稍微老一點(diǎn)的版本才有,最新的是沒有的 */ // 如果有如下內(nèi)容状植,注釋掉: try: final_flags.remove( '-stdlib=libc++' ) except ValueError: pass // 注釋完后變成下面這樣 #try: # final_flags.remove( '-stdlib=libc++' ) #except ValueError: # pass
-
.vimrc
中的配置" #####YouCompleteMe Configure let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' " 自動(dòng)補(bǔ)全配置 set completeopt=longest,menu "讓Vim的補(bǔ)全菜單行為與一般IDE一致(參考VimTip1228) autocmd InsertLeave * if pumvisible() == 0|pclose|endif "離開插入模式后自動(dòng)關(guān)閉預(yù)覽窗口 inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" "回車即選中當(dāng)前項(xiàng) "上下左右鍵的行為 會(huì)顯示其他信息 "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 默認(rèn)tab s-tab 和自動(dòng)補(bǔ)全沖突 "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 "關(guān)閉加載.ycm_extra_conf.py提示 let g:ycm_collect_identifiers_from_tags_files=1 " 開啟 YCM 基于標(biāo)簽引擎 let g:ycm_min_num_of_chars_for_completion=2 " 從第2個(gè)鍵入字符就開始羅列匹配項(xiàng) let g:ycm_cache_omnifunc=0 " 禁止緩存匹配項(xiàng),每次都重新生成匹配項(xiàng) let g:ycm_seed_identifiers_with_syntax=1 " 語法關(guān)鍵字補(bǔ)全 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> "在注釋輸入中也能補(bǔ)全 let g:ycm_complete_in_comments = 1 "在字符串輸入中也能補(bǔ)全 let g:ycm_complete_in_strings = 1 "注釋和字符串中的文字也會(huì)被收入補(bǔ)全 let g:ycm_collect_identifiers_from_comments_and_strings = 0 let g:clang_user_options='|| exit 0' "nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR> " 跳轉(zhuǎn)到定義處 " #####YouCompleteMe Configure
-
.ycm_extra_conf.py
中的配置-
用命令查看庫路徑
echo | clang -v -E -x c++ -
結(jié)果可能如下: 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 太長了浊竟,這里省略一部分中間內(nèi)容怨喘;.........表示生咯的內(nèi)容 "/usr/local/bin/clang" -cc1 -triple ......... -mstackrealign -fobjc-runtime=gcc 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 "<stdin>" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 318 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "<stdin>" 2
-
整理上述內(nèi)容,并添加到flag中
將以上內(nèi)容復(fù)制出來,修改成如下: '-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',
-
補(bǔ)全 C 語言全局函數(shù)問題(vim ~/.vimrc文件修改)
默認(rèn)情況下輸入 ., ->, :: 之后會(huì)觸發(fā)補(bǔ)全函數(shù)和類振定, 但是默認(rèn)情況下是不補(bǔ)全全局函數(shù)的必怜,所以 C 語言中的 printf 之類的函數(shù)就無法補(bǔ)全
解決辦法就是手動(dòng)調(diào)用補(bǔ)全,對(duì)應(yīng)的 YCM 函數(shù)是
ycm_key_invoke_completion
后频,將其綁定到快捷鍵 letg:ycm_key_invoke_completion = '<C-a>'
(默認(rèn)是 <C-Space>)
編譯安裝clang+llvm
Clang和LLVM的關(guān)系
Clang和LLVM到底是什么關(guān)系梳庆,這是在研究Clang的過程中所不可避免的一個(gè)問題。如果要搞清楚Clang和LLVM之間的關(guān)系卑惜,首先先要知道 宏觀的LLVM 和 微觀的LLVM 膏执。
宏觀的LLVM ,指的是整個(gè)的LLVM的框架露久,它肯定包含了Clang胧后,因?yàn)镃lang是LLVM的框架的一部分,是它的一個(gè)C/C++的前端抱环。雖然這個(gè)前端占的比重比較大蜡塌,但是它依然只是個(gè)前端协饲,LLVM框架可以有很多個(gè)前端和很多個(gè)后端者祖,只要你想繼續(xù)擴(kuò)展恍飘。
微觀的LLVM ,指的是以實(shí)際開發(fā)過程中梯啤,包括實(shí)際使用過程中竖伯,劃分出來的LLVM。比如編譯LLVM和Clang的時(shí)候因宇,LLVM的源碼包是不包含Clang的源碼包的七婴,需要單獨(dú)下載Clang的源碼包。
所以這里想討論的是 微觀的 LLVM和Clang的關(guān)系察滑。從編譯器用戶的角度打厘,Clang使用了LLVM中的一些功能,目前所知道的主要就是對(duì)中間格式代碼的優(yōu)化贺辰,或許還有一部分生成代碼的功能户盯。從Clang和微觀LLVM的源碼位置可以看出,Clang是基于微觀的LLVM的一個(gè)工具饲化。而 從功能的角度來說莽鸭,微觀的LLVM可以認(rèn)為是一個(gè)編譯器的后端,而Clang是一個(gè)編譯器的前端 吃靠。
編譯安裝
先說一下目錄結(jié)構(gòu):
llvm
tools
clang(cfe)
tools
extra(clang-tools-extra)
projects
compiler-rt
-
下載llvm的源代碼
wget http://llvm.org/releases/3.9.0/llvm-3.9.0.src.tar.xz tar xf llvm-3.9.0.src.tar.xz mv llvm-3.9.0.src llvm
-
下載clang的源代碼
cd llvm/tools wget http://llvm.org/releases/3.9.0/cfe-3.9.0.src.tar.xz tar xf cfe-3.9.0.src.tar.xz mv cfe-3.9.0.src clang cd ../..
-
下載clang-tools-extra的源代碼
cd llvm/tools/clang/tools wget http://llvm.org/releases/3.9.0/clang-tools-extra-3.9.0.src.tar.xz tar xf clang-tools-extra-3.9.0.src.tar.xz mv clang-tools-extra-3.9.0.src extra cd ../../../..
-
下載compiler-rt的源代碼
cd llvm/projects wget http://llvm.org/releases/3.9.0/compiler-rt-3.9.0.src.tar.xz tar xf compiler-rt-3.9.0.src.tar.xz mv compiler-rt-3.9.0.src compiler-rt cd ../..
這樣之后 clang硫眨,clang-tool-extra 和 compiler-rt 就可以和 llvm 一起編譯了。
-
編譯安裝
注意: 3.7.0以后巢块,不允許在源碼樹中進(jìn)行構(gòu)建礁阁,我們可以在llvm源碼目錄同級(jí)目錄中創(chuàng)建一個(gè)目錄build目錄巧号,然后使用絕對(duì)路徑進(jìn)行構(gòu)建
./configure --enable-optimized --enable-targets=host-only --prefix=/home/YouPathToInstall/llvm
最后一個(gè)編譯選項(xiàng)時(shí)指定編譯路徑,前面兩個(gè)也盡量加上氮兵。
不然編譯時(shí)間的時(shí)候會(huì)時(shí)間很長而且占很大的文件空間裂逐。make -j4 // 編譯使用4核cpu一起編譯,加快編譯速度 make install
檢查是否安裝成功:
clang –version
-
完畢之后
cd ~/.vim/bundle/YouCompleteMe mkdir ~/build cd ~/build #libclang.so 這個(gè)路徑,查詢的時(shí)候updatedb更新locate的數(shù)據(jù) locate libclang.so ~/.vim/bundle/YouCompleteMe/third_party/ycmd/libclang.so ~/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
要注意的是-DEXTERNAL_LIBCLANG_PATH這個(gè)參數(shù)歹鱼,用于指定libclang.so的位置泣栈。如果不指定的話,YCM將無法正常工作弥姻,總是報(bào):The YCM shut down, crash report...之類的錯(cuò)誤南片。而這個(gè)libclang.so就是我們?cè)诰幾gClang的時(shí)候生成的。因此庭敦,相關(guān)路徑也即
~/clang/build/Release+Asserts/lib/libclang.so
需要替換成自己對(duì)應(yīng)的路徑疼进。