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__

Sunday, April 19, 2015

Dijkstra Algorithm

http://www.algolist.com/code/cpp/Dijkstra%27s_algorithm

static vs shared libraries

Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files. All the code relating to the library is in this file, and it is referenced by programs using it at run-time. A program using a shared library only makes reference to the code that it uses in the shared library.

Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one].

There are advantages and disadvantages in each method.

Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.

Static libraries increase the overall size of the binary, but it means that you don't need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.

http://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries