Difference between revisions of "C/C++"

From HCL
Jump to: navigation, search
 
(26 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Coding ==
 
== 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 [http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml Google C++ Style Guide]
 
* [http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS One-true-brace ident style]
 
* [http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS One-true-brace ident style]
 
* [http://en.wikipedia.org/wiki/Pragma_once Coding header files]
 
* [http://en.wikipedia.org/wiki/Pragma_once Coding header files]
 +
* Learn from examples and use coding approaches from third-party software
  
 
== Commenting ==
 
== Commenting ==
Line 9: Line 11:
 
== C++ ==
 
== C++ ==
 
* [http://developers.sun.com/solaris/articles/mixing.html Mixing C/C++]
 
* [http://developers.sun.com/solaris/articles/mixing.html Mixing C/C++]
* [http://en.wikipedia.org/wiki/Template_metaprogramming Template C++]
+
* Provide main API in C
 +
* Use plain C unless you need flexible data structures or [[STL]]/[[Boost]] functionality
 +
* [http://en.wikipedia.org/wiki/Template_metaprogramming Template C++] is preferrable from the point of view of runtime performance
 +
* Mind the life cycle of objects: [http://en.wikipedia.org/wiki/Default_constructor Default constructor] [http://en.wikipedia.org/wiki/Copy_constructor Copy constructor], [http://en.wikipedia.org/wiki/Destructor_(computer_science) Destructor]
 +
* [http://www.gnu.org/software/hello/manual/automake/Libtool-Convenience-Libraries.html Force C++ linking]
  
== General ==
+
== Tips & Tricks  ==
* Don't use non-standard functions, like [http://en.wikipedia.org/wiki/Itoa itoa]
+
 
* [http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries]
+
*[http://www.gnu.org/s/libc/manual/html_node/Date-and-Time.html#Date-and-Time Timing in C]
 +
*Don't use non-standard functions, like [http://en.wikipedia.org/wiki/Itoa itoa]  
 +
*[http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html Handling program arguments] (avoid <code>argp</code> since it is not supported on many platforms)
 +
*[http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries]  
 +
*Avoid [http://en.wikipedia.org/wiki/Variable-length_array variable-length arrays]. First, GCC allocates them on the stack. Second, the status of this feature in GCC is BROKEN. Therefore, never do this:
 +
 
 +
<source lang="C">
 +
  int size;
 +
  MPI_Comm_size(MPI_COMM_WORLD, &size);
 +
  char names[size][MPI_MAX_PROCESSOR_NAME];
 +
</source>
 +
 
 +
*Implement delays in the execution of the program with help of [http://linux.die.net/man/2/nanosleep 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 [http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Spaces_vs._Tabs#Spaces_vs._Tabs 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 ([http://stackoverflow.com/questions/3218528/indenting-in-vim-with-all-the-files-in-folder 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)

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)