Sunday, October 4, 2015

Format Your C++ Code

Clang-format

It supports [1] llvm /clang [2] google [3] chromium coding styles.

$sudo apt-get install clang-format-3.5

you can check the supported coding style by
$clang-format-3.5  -h

Now you need to generate .clang-fromat file.
$clang-format-3.5  -style=Google -dump-config > .clang-format

Put .clang-format file in your project directory,
$clang-format-3.5 -style=fille

Clang-format + Vim

Download the python script from the link below, if you didn't install clang-format-3.5 as before.
http://clang.llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py

If you sudo installed clang-format-3.5, you will find your py script is located at /usr/share/vim/addons/syntax/

open your vimrc file
$vim .vimrc

add the following lines to vimrc
map <C-K> :pyf /usr/share/vim/addons/syntax/clang-format-3.5.py<cr>
imap <C-K> <ESC>:pyf /usr/share/vim/addons/syntax/clang-format-3.5.py<cr>

Now you should be able to use control + k to format the codes, in normal / visual / insert modes.
(http://mesos.apache.org/documentation/latest/clang-format/)

As default, it uses LLVM style.




astyle



[1] Download source file
to get linux version
wget -c http://skylineservers.dl.sourceforge.net/project/astyle/astyle/astyle%202.05.1/astyle_2.05.1_linux.tar.gz


[2] cd to the build/gcc folder
~/Downloads/astyle/build/gcc


type
$make
$sudo make install
$sudo ldconfig

[3]usage
$astyle --style=google test.cpp


You can use the cpplint.py to confirm the same format.

You can make alias in bashrc to make life easier.
alias googlestyle='astyle --style=google'

No comments:

Post a Comment