欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

windows下在vim中搭建c語(yǔ)言開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程

 更新時(shí)間:2021年05月11日 09:34:05   作者:其鑠  
這篇文章主要介紹了windows下在vim中搭建c語(yǔ)言開(kāi)發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1 代碼格式化

C語(yǔ)言代碼的格式化需要使用clang-format,而clang-format被集成在了llvm中,所以需要先安裝llvm,點(diǎn)擊此處下載

在這里插入圖片描述

下載之后運(yùn)行安裝文件,將其中的bin目錄添加到環(huán)境變量path中(需重啟電腦使新添加的環(huán)境變量生效)。例如我安裝后的目錄為C:\wsr\LLVM\bin,圖中的clang-format就是格式化c代碼需要的組件

在這里插入圖片描述
在這里插入圖片描述

1.1 clang-format初體驗(yàn)

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;
}

打開(kāi)powershell或cmd,設(shè)置環(huán)境變量后沒(méi)有重啟電腦需要進(jìn)入llvm的bin目錄,運(yùn)行如下命令:

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

  1. -style 表示風(fēng)格,可選項(xiàng)為 LLVM、Google、Chromium、Mozilla、WebKit 和 file,其中 file 指定參數(shù)文件。
  2. -i 表示將格式化后的內(nèi)容寫(xiě)入原文件。

如:

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

首先,打開(kāi)vim,在命令行模式下輸入:version,查看所使用的vim是否支持python,以及支持的python版本

在這里插入圖片描述

在這里插入圖片描述

從輸出可以看出,我用的vim是32位的vim8.2,此版本支持python,且所用的python版本為python2.7及python3.6。由于配置環(huán)境過(guò)程中用到的插件與vim使用的python版本有關(guān),所以下面先為vim配置python。

打開(kāi)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
下載好之后將兩個(gè)版本的python分別安裝(若已安裝過(guò)其他版本python不用卸載)。這里安裝時(shí)自定義安裝目錄,不要為其設(shè)置環(huán)境變量。

我的自定義安裝目錄如下所示:

C:\wsr (x86)\python\python27
C:\wsr (x86)\python\python36

編輯vim的配置文件.gvimrc,在其中添加如下設(shè)置項(xiàng)(安裝目錄中有空格的話,在空格前添加一個(gè)\):

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,使改動(dòng)生效。

1.2.2 安裝代碼格式化插件

安裝代碼格式化插件vim-clang-format,打開(kāi)powershell,執(zhí)行命令:

PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/rhysd/vim-clang-format.git vim-clang-format

注:這里沒(méi)有使用插件管理器安裝插件

仍然以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設(shè)置快捷鍵

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 代碼自動(dòng)補(bǔ)全

代碼自動(dòng)補(bǔ)全需要安裝插件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中使用代碼自動(dòng)補(bǔ)全了。使用vim編輯test2.c

輸入 inc、按Tab鍵
輸入 main、按Tab鍵
輸入 printf、按Tab鍵
……

如果想自定義代碼片段的話,可以在c:\users\username\vimfiles文件夾中新建文件夾ultisnips,并在其中新建文件c.snippets、cpp.snippets……
c.snippets對(duì)應(yīng)C語(yǔ)言、cpp.snippets對(duì)應(yīng)C++……

在這里插入圖片描述

其中文件夾的名字自定義,若有多個(gè)文件夾,可在.gvimrc添加如下設(shè)置

let g:UltiSnipsSnippetDirectories=["ultisnips","ultisnips-1"]

在這里插入圖片描述

其中自定義代碼片段的格式為:

snippet trigger_word [ "description" [ options ] ]

snippet 縮寫(xiě) [ “描述” [選項(xiàng)] ]
code
endsnippet

C:\Users\fy\vimfiles\ultisnips\c.snippets

snippet if0
if ($1 == 0) {
	${2}
}
endsnippet

定義上述代碼片段之后,在C源文件中編輯代碼時(shí)輸入if0,再按下Tab就能添加代碼了。

3 編譯運(yùn)行源程序

.gvimrc中添加如下設(shè)置:

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就能一鍵編譯運(yùn)行C程序了

到此這篇關(guān)于windows下在vim中搭建c語(yǔ)言開(kāi)發(fā)環(huán)境的文章就介紹到這了,更多相關(guān)vim中搭建c語(yǔ)言開(kāi)發(fā)環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論