Back to CFM home             Brown University

Programming in C++

C++ is the emerging standard for applications programming and has become a requirement for most programmers in this decade. It is generally speaking a superset of C, at a higher level and of course an object-oriented language. While some of its features (especially operator overloading & inheritance) make it ideal for the concise and high-level expression of mathematical algorithms and hence very attractive for the implementation of scientific codes, the current generation of C++ compilers isn't very good at optimizing C++ code, making it slower than equivalent C and Fortran code most of the time. This is probably going to change soon though. Meanwhile one can still mix C++ and Fortran. C++ compilers are a lot more fussy than C ones so far as correct and consistent prototyping is correct so more attention has to be given to the matter. Make sure to enclose your C prototyping with an extern "C" {}. C++ has a standard complex arithmetic class thus doing away with one of the major disadvantages of C wrt. numerical codes.

  1. Compiling & Linking
  2. To compile a C++ program in file c++_program.C (note C++ programs on most systems need to be in files with a .C extension - on others it is .cc or .c++, small case or large. Look at your C++ compiler's man page for more information on the subject):
    	% CC c++_program.C -o executable_name
    If you omit -o executable_name the executable binary will be called a.out by default.
    If your program is split over more than one file, you can either compile them all on the same line:
    	% CC c++_program1.C c++_program2.C -o executable_name
    or compile them separately and then link the resulting object files (".o" files) together:
    	% CC -c c++_program1.C
    	% CC -c c++_program2.C
    	% CC c++_program1.o c++_program2.o -o executable_name
    If some of the include files that you require (by # include <include_file.h> statements in your C++ source code) are not in the standard include file search paths the C++ preprocessor cpp (called by the C++ compiler automatically) searches in, you can specify them by the -Iinclude_search_path flag:
    	% CC -I/usr/local/mpich/include -c mpi.C 
    Similarly, if some of the librariy functions that your program uses are not to be found among the standard libraries the linker looks in for a C++ program, you have to specify them yourself, sometimes including the path to the library if it not in the standard directory the linker looks in:
    	% CC mathc++_program1.o mathc++_program2.o -o executable_name -lm
    	% CC mpi.o -o mpi-test -L/usr/local/mpich/lib/IRIX/ch_shmem -lmpi -lm
    If you are using a library whose filename is libblas.a, you specify it as -lblas.

  3. Compilers
  4. CC is the standard name for a C++ compiler but not the only option:

  5. Optimization flags
  6. Please make sure that once you've developed and debugged a code you compile it optimized for any production runs you make. Running unoptimized production code is a waste of your time as well as well as CFM computing resources. One only needs to take care when using optimization flags as high optimization levels can alter the semantics of your code and produce significantly different and hence erroneous results. You should check the respective man pages for each compiler to see which optimization flags pose such a threat. In that case it is necessary to test your optimized code by comparing the results of one or more of its runs with those of the code compiled with no optimization. If the difference in the results is small (machine or algorithmic accuracy) then go ahead and use the optimized code. If the difference is large enough for the results to be wrong, choose a lower optimization level and try again. Despite the extra trouble you may have to go through, please try and compile your code optimized, you may be very surprised by how much the time it takes to run (especially if it is well written) decreases! And of course always remember that usually the best optimization is a better algorithm. It is suggested that you look up the man pages for the compiler you plan to use for the best results. In the case of C++ there are a lot of options related to virtual tables and inline functions that you need to decide upon. (Note that sometimes even supposedly safe optimization options could cause problems due to bugs in the optimizer.) Suggestions for optimization flags for the C++ compilers on our systems are as follows:
  7. On the IBMs:

  8. For more C++ related information