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'

Saturday, October 3, 2015

Preprocessor directives

Refer to (http://www.cplusplus.com/doc/tutorial/preprocessor/)

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#).
Using backslash (\) to continue the current line.

It includes :
[1] macro definitions
#define, #undef

[2] conditional inclusions
#ifdef, #ifndef, #if, #endif, #else and #elif

[3] line control
#line

[4] error directive
#error

[5] source file inclusion
#include

[6] pragma directive
#pragma

[7] predefined macro names
__LINE__
__FILE__
__DATE__
__TIME__