Showing posts with label UNIX/Linux. Show all posts
Showing posts with label UNIX/Linux. Show all posts

Saturday, December 25, 2010

g++ Compiler Options

Always strive to compile code using the highest warning level available for your compiler and eliminate warnings by modifying the code. This really helps avoid all those hours of debugging and frustration - at the end of which you discover that all the mistake you did was a silly function returning no value in some code path or forgot to add default case to switch statement!

In this article, I will present a whole list of options for g++ compiler. I have borrowed the detailed description of these options from gcc/g++ online manual. In case of any doubt, please consult relevant documentation on your system specific to your compiler version.


-D_GLIBCXX_DEBUG -g -Wall -Wextra -pedantic  -Weffc++ -Wold-style-cast -Woverloaded-virtual -Wswitch-default -Wswitch-enum -Wmissing-noreturn -Wunreachable-code –Winline


Description:


-D_GLIBCXX_DEBUG  :
This is the libstdc++ debug mode which replaces unsafe (but efficient) standard containers and iterators with semantically equivalent safe standard containers and iterators to aid in debugging user programs.

-g :
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

Unlike most other C compilers, GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs. 


-Wall :  Enable all warnings. Consult manual for more details.


-Wextra : Enable some extra warnings in addition to -Wall

-pedantic : Issue all the warnings demanded by strict ISO C and ISO C++

-Weffc++ :
Warn about violations of the following style guidelines from Scott Meyers' Effective C++ book:

    * Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.
    * Item 12: Prefer initialization to assignment in constructors.
    * Item 14: Make destructors virtual in base classes.
    * Item 15: Have operator= return a reference to *this.
    * Item 23: Don't try to return a reference when you must return an object.

Also warn about violations of the following style guidelines from Scott Meyers' More Effective C++ book:

    * Item 6: Distinguish between prefix and postfix forms of increment and decrement operators.
    * Item 7: Never overload &&, ||, or ,.

When selecting this option, be aware that the standard library headers do not obey all of these guidelines; use grep -v to filter out those warnings.

-Wold-style-cast :
Warn if an old-style (C-style) cast to a non-void type is used within a C++ program. The new-style casts (static_cast, reinterpret_cast, and const_cast) are less vulnerable to unintended effects and much easier to search for.

-Woverloaded-virtual: Warn when a function declaration hides virtual functions from a base class.

-Wswitch-default : Warn whenever a switch statement does not have a default case.

-Wswitch-enum : Warn whenever a switch statement has an index of enumeral type and lacks a case for one or more of the named codes of that enumeration. case labels outside the enumeration range also provoke warnings when this option is used.

-Wmissing-noreturn :
Warn about functions which might be candidates for attribute noreturn. Note these are only possible candidates, not absolute ones. Care should be taken to manually verify functions actually do not ever return before adding the noreturn attribute, otherwise subtle code generation bugs could be introduced. You will not get a warning for main in hosted C environments.

-Wunreachable-code :
Warn if the compiler detects that code will never be executed.

This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.
It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.
For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.

–Winline : Warn if a function can not be inlined and it was declared as inline. Even with this option, the compiler will not warn about failures to inline functions declared in system headers

If you have any questions/suggestions do let me know by leaving a comment. Thanks. Enjoy your time with g++!

Wednesday, November 10, 2010

CScope and CTags

CSCOPE:
Cscope can be a particularly useful tool if you need to wade into a large code base. You can save yourself a lot of time by being able to do fast, targeted searches rather than randomly grepping through the source files by hand (especially since grep starts to take a while with a truly large code base).

Steps:
1. Get the source. First get the source code.
2. Figure out where you want to put your Cscope database files.
3. Generate cscope.files with a list of files to be scanned.

   find . -name '*.h' > cscope.files
   find . -name '*.cpp' >> cscope.files


4. Generate the Cscope database.
   cscope -b -q

The -b flag tells Cscope to just build the database, and not launch the Cscope GUI. The -q causes an additional, 'inverted index' file to be created, which makes searches run much faster for large databases.

5. Using the database
Append following line to your ~/.bashrc:
alias csd='cscope -d'

This tells Cscope not to regenerate the database. Otherwise you'll have to wait while Cscope checks for modified files, which can take a while for large projects, even when no files have changed. If you accidentally run 'cscope', without any flags, you will also cause the database to be recreated from scratch without the fast index or kernel modes being used, so you'll probably need to rerun your original cscope command above to correctly recreate the database.


Now, use command: csd


6. Regenerating the database when the source code changes.
If there are new files in your project, rerun your 'find' command to update cscope.files if you're using it.

Then simply invoke cscope the same way (and in the same directory) as you did to generate the database initially (i.e. cscope -b -q)


CTAGS:
Here are few useful tips. Refer to the manual page for more details using "man 1 ctags"

1. Build tags database:
ctags -R *
It creates a tags file

Create a tags file for Perl
ctags --languages=Perl -R

2. Add it to your editor (in ~/.vimrc file in my case):
set tags={Path to your view (since you might want to have separate tags file for each view)}/tags

3. Other options:
--exclude=[pattern] 
Add pattern to a list of excluded files and directories. This is used to avoid creating tags on specified files or files under directories.

Tuesday, October 05, 2010

GDB Essential commands


Command Abbr Description
set args
set command args. Also can do: gdb --args command arg1 ...
break b set breakpoint (at function, line number, ...)
run r (re)start execution
continue c Continue execution
step s Next line
next n next line without recursing into functions
finish fin next line after this function returns
list l show source (for line, function, offset, ...)
backtrace bt Show the stack of functions. Add "full" to include local variables
up, down, frame up, down, f Move between current stack frames
watch wa break when variable changes value
display

disp display expression each time program stops

info locals i loc display local variables
info threads i thr Display all threads
thread thr Switch to thread #
info breakpoints i b Display all breakpoints
Delete, enable, disable d, en, dis Delete, enable, disable breakpoint
help h display online help
focus next fs n switch window (allows cursor keys in CMD window for e.g.)
Ctrl-x a
Display code in another window
Ctrl-L
redraw the display (if program outputs for example)
print p Print value of expression
set variable set v Evaluate expression EXP and assign result to variable VAR
x/FMT x/FMT Examine memory


Sample .gdbinit file: 

# Set verbose printing of informational messages.
set verbose on
# Set printing of addresses
set print address on
# Set printing of object's derived type based on vtable info
set print object on
set print sym on
# Set prettyprinting of structures
#set print pretty off
# Set printing of C++ static members
set print static-members on
# Set demangling of encoded C++/ObjC names when displaying symbols
set print demangle on
# Unset printing of 8-bit characters in strings as \nnn
set print sevenbit-strings off
# Set prettyprinting of arrays
set print array on
# Set printing of array indexes
set print array-indexes
# Set printing of char arrays to stop at first null char
set print null-stop on
# Set printing of unions interior to structures
set print union on
# Set printing of C++ virtual function tables
set print vtbl on
# Set saving of the history record on exit
set history save on
# Set history expansion on command input
set history expansion on
# Set gdb's prompt
set prompt (onkar)

handle SIGCONT nostop

#### OTHER OPTIONAL SETTINGS ####
# Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements
# set by the set print elements command. This limit also applies to the display of strings. When GDB starts, this limit is set to 200. Setting number-of-elements
# to zero means that the printing is unlimited.
#set print elements number-of-elements


#source ~/stl-views-1.0.3.gdb

#set history filename     # TODO: enable this if reqd. Set the filename in which to record the command history

#catch throw

Useful commands:

Conditional breakpoint:

break main.cc:100 if i == 10
 

Repetitive commands:

b main()
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>print i
>print j
>print k
>end

The directory command and setting source directory:

(gdb) directory ~/src/somepackage/src
Source directories searched: /home/nelhage/src/coreutils-7.4:$cdir:$cwd 
 
This requests gdb to search for source files in the given dir in addition to 
the existing directories.

Tuesday, September 28, 2010

GNU Autotools - Autoconf, Automake tutorial

The GNU build system, also known as the Autotools, is a suite of programming tools designed to assist in making source-code packages portable to many Unix-like systems.

Here is a sample step by step example to build a standard GNU project from scratch:

1. README contains some very limited documentation for our little package.
[onkar@gnutools queue]$ cat README
This is a demonstration package for GNU Automake.
Type `info Automake' to read the Automake manual.



2. Makefile.am and src/Makefile.am contain Automake instructions for these two directories.
[onkar@gnutools queue]$ cat src/Makefile.am
bin_PROGRAMS = queue
queue_SOURCES = main.cpp Queue.cpp
noinst_HEADERS = Queue.h

[onkar@gnutools queue]$ cat Makefile.am
SUBDIRS = src
docdir = ${datadir}/doc/${PACKAGE}
dist_doc_DATA = README




3. configure.ac contains Autoconf instructions to create the configure script.

[onkar@gnutools queue]$ cat configure.ac
AC_INIT([queue], [1.0], [bug-automake@gnu.org])
AC_PROG_CXX
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
 Makefile
 src/Makefile
])
AC_OUTPUT



4. autoreconf: Once you have these five files, it is time to run the Autotools to instantiate the build system. Do this using the autoreconf command as follows:
[onkar@gnutools queue]$ autoreconf --install



5. configure: You can see that autoreconf created four other files: configure, config.h.in, Makefile.in, and src/Makefile.in. The latter three files are templates that will be adapted to the system by configure under the names config.h, Makefile, and src/Makefile. Let's do this:
[onkar@gnutools queue]$ ./configure
checking for g++... g++
checking for C++ compiler default output file name... a.out
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands


6. make, make clean, make distcheck: You can see Makefile, src/Makefile, and config.h being created at the end after configure has probed the system. It is now possible to run all the targets we wish. For instance:
[onkar@gnutools queue]$ make
make  all-recursive
make[1]: Entering directory `/home/onkar/practice/cc/queue'
Making all in src
make[2]: Entering directory `/home/onkar/practice/cc/queue/src'
if g++ -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.cpp; \
then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi
if g++ -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT Queue.o -MD -MP -MF ".deps/Queue.Tpo" -c -o Queue.o Queue.cpp; \
then mv -f ".deps/Queue.Tpo" ".deps/Queue.Po"; else rm -f ".deps/Queue.Tpo"; exit 1; fi
g++  -g -O2   -o queue  main.o Queue.o
make[2]: Leaving directory `/home/onkar/practice/cc/queue/src'
make[2]: Entering directory `/home/onkar/practice/cc/queue'
make[2]: Leaving directory `/home/onkar/practice/cc/queue'
make[1]: Leaving directory `/home/onkar/practice/cc/queue'


[onkar@gnutools queue]$ make distcheck
..................
===========================================
queue-1.0 archives ready for distribution:
queue-1.0.tar.gz
===========================================


[onkar@gnutools queue]$ make clean
Making clean in src
make[1]: Entering directory `/home/onkar/practice/cc/queue/src'
test -z "queue" || rm -f queue
rm -f *.o
make[1]: Leaving directory `/home/onkar/practice/cc/queue/src'
Making clean in .
make[1]: Entering directory `/home/onkar/practice/cc/queue'
make[1]: Nothing to be done for `clean-am'.
make[1]: Leaving directory `/home/onkar/practice/cc/queue'


Done!

Wednesday, March 17, 2010

Power of UNIX

UNIX/Linux is a very powerful operating system with a lot of simple commands for small tasks. Power of UNIX lies in its ability to handle very complex, huge data requirements which would bring other operating systems to a screeching halt.


For example from my personal experience, simple utilities like grep, gzip, gunzip, split are able to handle data varying from 1 KB to GBs with the same elegance and accuracy.

Say we have a file of size around 36 GB.

Follwing operations are performed on that file:
1. gzip   : size comes down to 4.2 GB.
2. gunzip : size grows back to 36 GB.
3. grep   : Search a pattern in this file and create a new file of size around 32 GBs => happens in around half an hour!
4. gzip   : compress it back and file size becomes 4.1 GB.

Alternately, you can use split command between step 2 and 3 to split the big file in no. of small chunks and then perform the remaining operations.



The operations we performed above are quite simple. However, the huge data set on which they are applied is what makes the problem hard. That's where power of UNIX comes...




Tuesday, March 09, 2010

Vim Tips

Very useful  Vi Tips:

* # g* g#        find word under cursor (forwards/backwards) or word completion in insert mode
guu                  lowercase line
gUU                uppercase line
~                     invert case (upper->lower; lower->upper) of current character
'.                      jump to last modification line
`.                     jump to exact spot in last modification line
:reg                 display contents of all registers
:"1p                 paste from register 1
gf                    Open the file under cursor
ctrl+w f          Open the file under cursor and split screen horizontally.
vim -O file1 file2    open file1 and file2 side by side
:command              runs named command
Ctrl+w {left,right,up,down}    move to window
''                        move to previous position
Ctrl+o  move to previous position (recursively, in history)
CTRL-I jump to next location (recursively, in history)

Jumping to diffs:
[c  Jump backwards to the previous start of a change.
]c  Jump forwards to the next start of a change.


Thursday, February 25, 2010

Sample vimrc file

The vimrc file contains optional runtime configuration settings to initialize Vim when it starts. On Unix based systems, the file is named .vimrc

Here is a sample .vimrc file I use.


"set smarttab       " When on, a in front of a line inserts blanks according to shiftwidth
set showmode        " Show the current mode
set showcmd         " display incomplete commands
set showmatch       " show matching braces,
set hlsearch        " highlight searches
"set title          " show title in console title bar
set laststatus=2    " always show status line
set ic              " case insensitive search : use set noic to turn it off

set nocompatible    " This setting prevents vim from emulating the original vi's bugs and limitations.

set autoindent
set smartindent
"   The first setting tells vim to use autoindent (that is, use the current line's indent level to set the indent level of new lines). The second makes vim attempt to intelligently guess the indent level of any new line based on the previous line, assuming the source file is in a C-like language. Combined, they are very useful in writing well-formatted source code.

set tabstop=2

set shiftwidth=2
"  The first setting sets up 2-space tabs, and the second tells vi to use 2 spaces when text is indented (auto or with the manual indent adjustmenters.)

set showmatch
"    This setting will cause the cursor to very briefly jump to a brace/parenthese/bracket's match whenever you type a closing or opening brace/parenthese/bracket. I've had almost no mismatched-punctuation errors since I started using this setting.

set vb t_vb=
"This setting prevents vi from making its annoying beeps when a command doesn't work. Instead, it briefly flashes the screen -- much less annoying.

set ruler
"    This setting ensures that each window contains a statusline that displays the current cursor position.


set incsearch
"    With this nifty option, vim will search for text as you enter it. For instance, if you type /bob to search for bob, vi will go to the first b after you type the b, to the first bo after you type the o, and so on. It makes searching much faster, since if you pay attention you never have to enter more than the minimum number of characters to find your target location. Make sure that you press Enter to accept the match after vim finds the location you want.

"set virtualedit=all
"    By default, vim doesn't let the cursor stray beyond the defined text. This setting allows the cursor to freely roam anywhere it likes in command mode. It feels weird at first but is quite useful.


set tags=/tags,./tags

set showfulltag         " Get function usage help automatically

""""""""""""" Highlight following in all files """"""""""""""""""""""
hi TODO ctermfg=red ctermbg=blue term=bold
syntax match TODO /TODO/




""""""""""""""""""""""""""""""""""""""""""
"        Use Cscope within Vi                        "

""""""""""""""""""""""""""""""""""""""""""" Refer http://vim.wikia.com/wiki/Cscope for more details

if has('cscope')
  set cscopetag cscopeverbose

  if has('quickfix')
    set cscopequickfix=s-,c-,d-,i-,t-,e-
  endif

  cnoreabbrev csa cs add
  cnoreabbrev csf cs find
  cnoreabbrev csk cs kill
  cnoreabbrev csr cs reset
  cnoreabbrev css cs show
  cnoreabbrev csh cs help

  command -nargs=0 Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src
endif
""""""""""""""""""""""""""""""""""""""""""

""""""""""""""""""""""""""""""""""""""""""
"               Mappings                 "
""""""""""""""""""""""""""""""""""""""""""
" Keyboard mappings
map :previous  " map F1 to open previous buffer
map :next      " map F2 to open next buffer
map :setl noai nocin nosi inde= " Disabling auto indent for the current file