Difference between revisions of "C/C++"

From HCL
Jump to: navigation, search
(General)
 
(5 intermediate revisions by 2 users not shown)
Line 17: Line 17:
 
* [http://www.gnu.org/software/hello/manual/automake/Libtool-Convenience-Libraries.html Force C++ linking]
 
* [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://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://www.gnu.org/s/libc/manual/html_node/Date-and-Time.html#Date-and-Time Timing in C]
* [http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries]
+
*Don't use non-standard functions, like [http://en.wikipedia.org/wiki/Itoa itoa]  
* 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:
+
*[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)  
<syntaxhighlight lang="C">
+
*[http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries]  
int size;
+
*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:
MPI_Comm_size(MPI_COMM_WORLD, &size);
+
 
char names[size][MPI_MAX_PROCESSOR_NAME];
+
<source lang="C">
</syntaxhighlight>
+
  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)