Difference between revisions of "C/C++"

From HCL
Jump to: navigation, search
(Tips & Tricks)
 
(One intermediate revision by the same user not shown)
Line 42: Line 42:
 
*To indent all .c and .h files with vim use the following ([http://stackoverflow.com/questions/3218528/indenting-in-vim-with-all-the-files-in-folder explained here]):
 
*To indent all .c and .h files with vim use the following ([http://stackoverflow.com/questions/3218528/indenting-in-vim-with-all-the-files-in-folder explained here]):
 
   :args ./*/*.[ch] | argdo execute "normal gg=G" | update
 
   :args ./*/*.[ch] | argdo execute "normal gg=G" | update
 +
or use the Unix command
 +
  $ indent
 +
 +
== Color GCC  ==
 +
Colours output of GCC so you can see errors and warnings.
 +
  sudo apt-get install colorgcc
 +
  ln -s /usr/bin/colorgcc ~/bin/gcc
 +
*Make sure ~/bin is in path _before_ gcc. (Add ~/bin to path in ~/.profile)

Latest revision as of 17:47, 9 April 2013

Coding

  • C++ programming style is preferrable. For example, in variable declarations, pointers and references should have their reference symbol next to the type rather than to the name. Variables should be initialized where they are declared, and should be declared where they are used. For more details, see Google C++ Style Guide
  • One-true-brace ident style
  • Coding header files
  • Learn from examples and use coding approaches from third-party software

Commenting

  • Place Doxygen comments in header files (before declarations of namespaces/classes/structs/typedefs/macros) and main source files (for documenting tools and tests)
  • Use double forward slash for short comments in the code

C++

Tips & Tricks

  int size;
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  char names[size][MPI_MAX_PROCESSOR_NAME];
  • Implement delays in the execution of the program with help of nanosleep. Compared to sleep and usleep, nanosleep has the advantage of not affecting any signals, it is standardized by POSIX, it provides higher timing resolution, and it allows to continue a sleep that has been interrupted by a signal more easily.
  • Indenting in fupermod is done in the google code style, two literal spaces, no tabs. To set vim to do this put the following in .vimrc:
 set autoindent
 set expandtab
 set tabstop=2
 set shiftwidth=2
 set softtabstop=2
  • To indent all .c and .h files with vim use the following (explained here):
 :args ./*/*.[ch] | argdo execute "normal gg=G" | update

or use the Unix command

 $ indent

Color GCC

Colours output of GCC so you can see errors and warnings.

 sudo apt-get install colorgcc
 ln -s /usr/bin/colorgcc ~/bin/gcc
  • Make sure ~/bin is in path _before_ gcc. (Add ~/bin to path in ~/.profile)