Sunday, December 30, 2012

gdb commands

bt - backtrace
frame number - select a particular stack frame
list - see the code
print var_name - print variable's value

Creating core dump

ulimit -c unlimited
To invesigate the core:
dbg program core_file

Monday, December 3, 2012

Using mintty with mingw

If you don't like default mingw console, you can use mintty instead.

mingw-get install mintty
Create link
C:\MinGW\msys\1.0\bin\mintty.exe /bin/env CHERE_INVOKING=1 /bin/bash -l
Source.

Thursday, November 15, 2012

Generating ctags and cscope tags for vim

I use this script for generate ctags and cscope tags. This MinGW version is to be run in source code directory.

#!/bin/sh
workdir=`pwd`
workdir=${workdir/\/c/c:}
echo Work directory: $workdir

if [ ! -d cscope ]
then
   echo Creating cscope directory
   mkdir cscope
fi

echo Generating ctags
cd $workdir
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

echo Generating cscope
cd /
find $workdir -name "*.cpp" -or -name "*.h" > $workdir/cscope/cscope.files
cd $workdir/cscope
cscope -bq

echo Complete
For running on Linux remove
workdir=${workdir/\/c/c:}

Sunday, November 11, 2012

Armadillo: C++ linear algebra library

Many C++ libraries for linear algebra, such as C++ versions of BLAS, LAPACK or uBlas from Boost are pretty hard for using by people who are not familiar with numerical methods. After some searching I have found out Armadillo library. It provides simple interface for BLAS and LAPACK functions with syntax similar to Matlab.

My .vimrc file

I use default vimrc file as a base for my own one. From vim you can get it with

:e $VIMRUNTIME/vimrc_example.vim

I always use tab expanding. So, usually a part of my .vimrc file looks like:

set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set textwidth=80
Specific values for tabstop, shiftwidth and textwidth depend on project.

It is useful, especially during code review, to see not expanded tabs and trailing spaces. Leaving trailing spaces in code is bad idea, because they often mess diffs. To print these characters:

set listchars=trail:~,tab:>-
set list

Set colorschemes for GUI and console:

if has("gui_running")
    colorscheme wombat
else
    colorscheme wombat256
endif

Remove superfluous GUI elements

" remove toolbar
set guioptions-=T
" remove menu bar
set go-=m
" remove right-hand scroll bar
set go-=r

You can use second key mapping and switch between keymaps with CTRL-^ in insert mode, for example:

" remove toolbar
set guioptions-=T
" remove menu bar
set go-=m
" remove right-hand scroll bar
set go-=r

Set font for GUI:

set guifont=Ubuntu\ Mono\ 12
I use Ubuntu Mono or Monospace on Linux and Consolas on Windows.

256 colors in console vim

To use 256 colors in vim it is needed to correctly set $TERM variable. For gnome-terminal it is gnome-256color. vim uses $TERM variable and terminfo files to determine terminal properties. To determine correct value of $TERM you can add to your .bashrc this code from vim.wikia.com:

if [ "$TERM" = "xterm" ] ; then
    if [ -z "$COLORTERM" ] ; then
        if [ -z "$XTERM_VERSION" ] ; then
            echo "Warning: Terminal wrongly calling itself 'xterm'."
        else
            case "$XTERM_VERSION" in
            "XTerm(256)") TERM="xterm-256color" ;;
            "XTerm(88)") TERM="xterm-88color" ;;
            "XTerm") ;;
            *)
                echo "Warning: Unrecognized XTERM_VERSION: $XTERM_VERSION"
                ;;
            esac
        fi
    else
        case "$COLORTERM" in
            gnome-terminal)
                # Those crafty Gnome folks require you to check COLORTERM,
                # but don't allow you to just *favor* the setting over TERM.
                # Instead you need to compare it and perform some guesses
                # based upon the value. This is, perhaps, too simplistic.
                TERM="gnome-256color"
                ;;
            *)
                echo "Warning: Unrecognized COLORTERM: $COLORTERM"
                ;;
        esac
    fi
fi

SCREEN_COLORS="`tput colors`"
if [ -z "$SCREEN_COLORS" ] ; then
    case "$TERM" in
        screen-*color-bce)
            echo "Unknown terminal $TERM. Falling back to 'screen-bce'."
            export TERM=screen-bce
            ;;
        *-88color)
            echo "Unknown terminal $TERM. Falling back to 'xterm-88color'."
            export TERM=xterm-88color
            ;;
        *-256color)
            echo "Unknown terminal $TERM. Falling back to 'xterm-256color'."
            export TERM=xterm-256color
            ;;
    esac
    SCREEN_COLORS=`tput colors`
fi

if [ -z "$SCREEN_COLORS" ] ; then
    case "$TERM" in
        gnome*|xterm*|konsole*|aterm|[Ee]term)
            echo "Unknown terminal $TERM. Falling back to 'xterm'."
            export TERM=xterm
            ;;
        rxvt*)
            echo "Unknown terminal $TERM. Falling back to 'rxvt'."
            export TERM=rxvt
            ;;
        screen*)
            echo "Unknown terminal $TERM. Falling back to 'screen'."
            export TERM=screen
            ;;
    esac
    SCREEN_COLORS=`tput colors`
fi

Also you have to have corresponding terminfo files. On Ubuntu you can install additional terminfo definitions:

sudo apt-get install ncurses-term
Now you have gnome-256color and other terminal definitions in /usr/share/terminfo (directory depends on system). You can use such themes as desert256, wombat256 etc.

To set colorscheme I use this code in my .vimrc:

if has("gui_running")
    colorscheme wombat
else
    colorscheme wombat256
endif

You can check how many colors are used inside vim by examining t_Co variable:

set t_Co
t_Co=256
I meet a difficulty with setting $TERM on one of Solaris servers I use. I had not colors in vim at all on this server. tput colors said me -1. There are not any terminfo file like *-256color and I have not root priviledges to add needed terminfo files. $TERM was set to xterm. But I found xtermc in terminfo directory. After setting $TERM to xtermc tput colors gives 8, and I can use syntax highlighting in vim, although not 256 colors.