windows下在vim中搭建c語言開發(fā)環(huán)境的詳細過程
1 代碼格式化
C語言代碼的格式化需要使用clang-format
,而clang-format
被集成在了llvm
中,所以需要先安裝llvm
,點擊此處下載
下載之后運行安裝文件,將其中的bin
目錄添加到環(huán)境變量path中(需重啟電腦使新添加的環(huán)境變量生效)。例如我安裝后的目錄為C:\wsr\LLVM\bin
,圖中的clang-format
就是格式化c代碼需要的組件
1.1 clang-format初體驗
test1.c
#include <stdio.h> int main(int argc, char *argv[]) { int a[2][2] = {{1,2} , {3,4}}; printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); return 0; }
打開powershell或cmd
,設置環(huán)境變量后沒有重啟電腦需要進入llvm的bin目錄
,運行如下命令:
PS C:\Users\fy> cd C:\wsr\LLVM\bin\ PS C:\wsr\LLVM\bin> .\clang-format.exe -style=google -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin>
test1.c
的源代碼被格式化為
#include <stdio.h> int main(int argc, char *argv[]) { int a[2][2] = {{1, 2}, {3, 4}}; printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); return 0; }
命令解釋
用法:clang-format -style=xxx -i ***.c
- -style 表示風格,可選項為 LLVM、Google、Chromium、Mozilla、WebKit 和 file,其中 file 指定參數(shù)文件。
- -i 表示將格式化后的內容寫入原文件。
如:
PS C:\wsr\LLVM\bin> .\clang-format -style=Chromium -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=LLVM -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=google -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=WebKit -i C:\Users\fy\Desktop\test1.c
1.2 在vim中格式化c代碼
1.2.1 為vim配置python
首先,打開vim
,在命令行模式下輸入:version
,查看所使用的vim是否支持python,以及支持的python版本
從輸出可以看出,我用的vim是32位的vim8.2
,此版本支持python
,且所用的python版本為python2.7及python3.6
。由于配置環(huán)境過程中用到的插件與vim使用的python版本有關,所以下面先為vim配置python。
打開python官網(wǎng),下載32位的python2.7及python3.6
,我下載的為2.7.18和3.6.8
:
https://www.python.org/ftp/python/3.6.8/python-3.6.8.exe
https://www.python.org/ftp/python/2.7.18/python-2.7.18.msi
下載好之后將兩個版本的python分別安裝(若已安裝過其他版本python不用卸載)。這里安裝時自定義安裝目錄,不要為其設置環(huán)境變量。
我的自定義安裝目錄如下所示:
C:\wsr (x86)\python\python27 C:\wsr (x86)\python\python36
編輯vim的配置文件.gvimrc
,在其中添加如下設置項(安裝目錄中有空格的話,在空格前添加一個\
):
set pythondll=C:\wsr\ (x86)\python\python27\python27.dll set pythonhome=C:\wsr\ (x86)\python\python27 set pythonthreedll=C:\wsr\ (x86)\python\python36\python36.dll set pythonthreehome=C:\wsr\ (x86)\python\python36
然后在vim命令行模式執(zhí)行:source $MYVIMRC
,使改動生效。
1.2.2 安裝代碼格式化插件
安裝代碼格式化插件vim-clang-format,打開powershell,執(zhí)行命令:
PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/rhysd/vim-clang-format.git vim-clang-format
注:這里沒有使用插件管理器安裝插件
仍然以test1.c
為例
#include <stdio.h> int main(int argc, char *argv[]) { int a[2][2] = {{1,2} , {3,4}}; printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); printf("Hello World!\n"); return 0; }
命令行模式下,執(zhí)行:ClangFormat
,則源文件就被格式化了
也可以在.gvimrc
中為vim-clang-format設置快捷鍵
let g:clang_format#style_options = { \ "AccessModifierOffset" : -4, \ "AlignConsecutiveAssignments": "true", \ "AlignConsecutiveDeclarations": "true", \ "AlignConsecutiveMacros": "true", \ "AlignOperands": "true", \ "AlignTrailingComments": "true", \ "AllowShortFunctionsOnASingleLine" : "Empty", \ "AllowShortIfStatementsOnASingleLine" : "true", \ "AllowShortLoopsOnASingleLine" : "true", \ "AlwaysBreakTemplateDeclarations" : "true", \ "BreakBeforeBraces" : "WebKit", \ "BreakBeforeTernaryOperators " : "true", \ "BreakStringLiterals" : "true", \ "ColumnLimit": 80, \ "MaxEmptyLinesToKeep": 1, \ "Standard" : "C++11"} " 使用 <Leader>cf 格式化代碼 autocmd FileType c,cpp,objc nnoremap <buffer><Leader>cf :<C-u>ClangFormat<CR> autocmd FileType c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR> " Toggle auto formatting: nmap <Leader>C :ClangFormatAutoToggle<CR>
2 代碼自動補全
代碼自動補全需要安裝插件vim-snippets、 ultisnips
PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/honza/vim-snippets.git vim-snippets PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/SirVer/ultisnips.git ultisnips
安裝完成之后,就可以在vim中使用代碼自動補全了。使用vim編輯test2.c
:
輸入 inc、按Tab鍵
輸入 main、按Tab鍵
輸入 printf、按Tab鍵
……
如果想自定義代碼片段的話,可以在c:\users\username\vimfiles
文件夾中新建文件夾ultisnips
,并在其中新建文件c.snippets、cpp.snippets
……
c.snippets對應C語言、cpp.snippets對應C++……
其中文件夾的名字自定義,若有多個文件夾,可在.gvimrc
添加如下設置
let g:UltiSnipsSnippetDirectories=["ultisnips","ultisnips-1"]
其中自定義代碼片段的格式為:
snippet trigger_word [ "description" [ options ] ] snippet 縮寫 [ “描述” [選項] ] code endsnippet
如C:\Users\fy\vimfiles\ultisnips\c.snippets
:
snippet if0 if ($1 == 0) { ${2} } endsnippet
定義上述代碼片段之后,在C源文件中編輯代碼時輸入if0
,再按下Tab
就能添加代碼了。
3 編譯運行源程序
在.gvimrc
中添加如下設置:
nnoremap <F5> :call CompileAndRun()<CR> func! CompileAndRun() exec "w" echo "Compiling..." if &filetype == 'c' exec "!gcc % -o %<" elseif &filetype == 'cpp' exec "!g++ % -o %<" endif echo "\nRunning..." exec "!%<" "exec "! ./%<" endfunc
更改生效后,按下F5
就能一鍵編譯運行C程序了
到此這篇關于windows下在vim中搭建c語言開發(fā)環(huán)境的文章就介紹到這了,更多相關vim中搭建c語言開發(fā)環(huán)境內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法
這篇文章主要為大家詳細介紹了C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01ros項目調試:vscode下配置開發(fā)ROS項目的詳細教程
這篇文章主要介紹了ros項目調試:vscode下配置開發(fā)ROS項目,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08