Compiling, linking, and running programs


Up: Programming Tools Next: Compiling and Linking without the Scripts Previous: Programming Tools

The mpich implementation provides four commands for compiling and linking C (mpicc), C++ (mpicxx), Fortran 77 (mpif77), and Fortran 90 (mpif90) programs.

Use these commands just like the usual C, Fortran 77, C++, or Fortran compilers. For example,

    mpicc -c foo.c  
    mpif77 -c foo.f 
    mpicxx -c foo.cxx 
    mpif90 -c foo.f 
and
    mpicc -o foo foo.o 
    mpif77 -o foo foo.o 
    mpicxx -o foo foo.o 
    mpif90 -o foo foo.o 
Commands for the linker may include additional libraries. For example, to use routines from the C math library library, use
    mpicc -o foo foo.o -lm 
Combining compilation and linking in a single command, as shown here,
    mpicc -o foo foo.c 
    mpif77 -o foo foo.f 
    mpicxx -o foo foo.cxx 
    mpif90 -o foo foo.f 
may also be used (on most systems).

Note that while the suffixes .c for C programs and .f for Fortran-77 programs are standard, there is no consensus for the suffixes for C++ and Fortran-90 programs. The ones shown here are accepted by many but not all systems. Mpich tries to determine the accepted suffixes, but may not always be able to.

Earlier versions of MPICH used mpiCC to compile C++ programs. However, on some operating systems, such as Mac OS X, filenames that differ only in case, such as mpicc and mpiCC, refer to the same file. To avoid problems caused when scripts and Makefiles that use the MPICH compilation scripts are ported to such systems, we have chnaged to using only mpicxx for compiling and linking C++ programs. We also encourage users to use .cxx or .cc as the file extension for C++ programs rather than .C (because it is the same as .c on OS X) or .cpp (because some applications use that suffix for files created with the C preprocessor).

You can override the choice of compiler by specifying the environment variable MPICH_CC, MPICH_F77, MPICH_CCC, or MPICH_F90. However, be warned that this will work only if the alternate compiler is compatible with the default one (by compatible, we mean that is uses the same sizes for all datatypes and layouts, and generates object code that can be used with the mpich libraries). If you wish to override the linker, use the environment variables MPICH_CLINKER, MPICH_F77LINKER, MPICH_CCLINKER, or MPICH_F90LINKER.

If you want to see the commands that would be used without actually running them, add the command line argument -show.

In addition, the following special options are supported for accessing some of the features of the MPE environment for monitoring MPI calls from within an application:

-mpilog
Build version that generates MPE log files.
-mpitrace
Build version that generates traces.
-mpianim
Build version that generates real-time animation.

These are described in more detail in Section Log and tracefile tools .



Up: Programming Tools Next: Compiling and Linking without the Scripts Previous: Programming Tools


Compiling and Linking without the Scripts


Up: Compiling, linking, and running programs Next: Running programs with mpirun Previous: Compiling, linking, and running programs

In some cases, it is not possible to use the scripts supplied by mpich for compiling and linking programs. For example, another tool may have its own compilation scripts. In this case, you can use -compile_info and -link_info to have the mpich compilation scripts indicate the compiler flags and linking libraries that are required for correct operation of the mpich routines. For example, when using the ch_shmem device on Solaris systems, the library thread (-lthread) must be linked with the application. If the thread library is not provided, the application will still link, but essential routines will be replaced with dummy versions contained within the Solaris C library, causing the application to fail.

For example, to determine the flags used to compile and link C programs, you can use these commands, whose output for the ch_p4 device on a Linux workstation is shown.

 % mpicc -compile_info 
 cc -DUSE_STDARG -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 
 -DHAVE_STDARG_H=1 -DUSE_STDARG=1 -DMALLOC_RET_VOID=1 
 -I/usr/local/mpich/include -c  
 
 % mpicc -link_info 
 cc -DUSE_STDARG -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 
 -DHAVE_STDARG_H=1 -DUSE_STDARG=1 -DMALLOC_RET_VOID=1 
 -L/usr/local/mpich/lib -lmpich  



Up: Compiling, linking, and running programs Next: Running programs with mpirun Previous: Compiling, linking, and running programs