Previous: , Up: Use of the PnetCDF Library   [Index]


1.8 Compiling and Linking with the PnetCDF Library

Details of how to compile and link a program that uses the PnetCDF C or FORTRAN interfaces differ, depending on the operating system, the available compilers, and where the PnetCDF library and include files are installed. Nevertheless, we provide here examples of how to compile and link a program that uses the PnetCDF library on a Unix platform, so that you can adjust these examples to fit your installation.

Building and Installing PnetCDF Library

Full instructions of building and installing PnetCDF library are described in the file named INSTALL come with every release of PnetCDF source codes. An example configure command and output are given below.

% ./configure --prefix=$HOME/PnetCDF/1.12.0 --enable-shared --with-mpi=/usr

------------------------------------------------------------------------------

   PnetCDF Version 1.12.0

   Features:  Build static libraries                      - yes
              Build shared libraries                      - yes
              Build Fortran APIs                          - yes
              Build C++ APIs                              - yes

   Compilers: MPICC    = /usr/bin/mpicc
              MPICXX   = /usr/bin/mpicxx
              MPIF77   = /usr/bin/mpif77
              MPIF90   = /usr/bin/mpif90
              CFLAGS   = -g -O2
              CXXFLAGS = -g -O2
              FFLAGS   = -g -O2
              FCFLAGS  = -g -O2

   Now run 'make' to build the library and utility tools.
   Then run 'make [<target>]' for testing and installation, where the
   optional <target> can be:
              tests    - build all test programs (build only, no run)
              check    - run sequential test programs
              ptest    - run parallel test programs on 4 MPI processes
              ptests   - run parallel test programs on 3,4,6,8 MPI processes
              install  - install PnetCDF library in $HOME/PnetCDF/1.12.0

------------------------------------------------------------------------------

In this example, configure command uses the MPI compilers installed under the folder of /usr, where the MPI header files can be found under /usr/include, MPI compiler commands and utility programs can be found under /usr/bin, and MPI library files can be found under /usr/lib. The location of PnetCDF library to be install is is specified by option –prefix which is the folder PnetCDF/1.12.0 under the user’s home folder.

Command to build PnetCDF library is

% make

Command to install PnetCDF library is

% make install

At the end of installation process, the following message should appear.

+----------------------------------------------------------------------------+
|
|  PnetCDF has been successfully installed under
|          $HOME/PnetCDF/1.12.0
|
|  * PnetCDF header files have been installed in
|          $HOME/PnetCDF/1.12.0/include
|  * PnetCDF library files have been installed in
|          $HOME/PnetCDF/1.12.0/lib
|  * PnetCDF utility programs have been installed in
|          $HOME/PnetCDF/1.12.0/bin
|  * PnetCDF man pages have been installed in
|          $HOME/PnetCDF/1.12.0/share/man
|
|  To compile your PnetCDF programs, please add the following to the command
|  line, so the compiler can find the PnetCDF header files:
|      -I$HOME/PnetCDF/1.12.0/include
|
|  Add the following line to link your program to PnetCDF library:
|      -L$HOME/PnetCDF/1.12.0/lib -lpnetcdf
|
|  Add the following to your run-time environment variable LD_LIBRARY_PATH,
|  when linking your executable with the PnetCDF shared libraries.
|      $HOME/PnetCDF/1.12.0/lib
|
|
|  PnetCDF is jointly developed by a team at Northwestern University and
|  Argonne National Laboratory.
|
|  Visit PnetCDF project web site for more information
|      https://parallel-netcdf.github.io
|
+----------------------------------------------------------------------------+

Compiling Your User Programs

Every C file that references PnetCDF functions or constants must contain an appropriate #include statement before the first such reference:

#include <pnetcdf.h>

For Fortran programs, the header file is pnetcdf.inc for Fortran 77 and the module file is pnetcdf.mod for Fortran 90 and later. The following code statement should be used in the user programs. For Fortran 77

include "pnetcdf.inc"

For Fortran 90 and later

use pnetcdf

These three files pnetcdf.h, pnetcdf.inc, and pnetcdf.mod are installed in the same location. Unless they are installed in a standard directory where the C and Fortran compilers always look, you must use the -I option when invoking the compiler, to specify a directory where pnetcdf.h is installed, for example:

/usr/bin/mpicc -O2 -c -I$HOME/PnetCDF/1.12.0/include myprogram.c

or for Fortran

/usr/bin/mpifort -O2 -c -I$HOME/PnetCDF/1.12.0/include myprogram.f

Unless the PnetCDF library is installed in a standard directory where the linker always looks, you must use the -L and -l options to link an object file that uses the PnetCDF library. For example:

/usr/bin/mpicc -O2 -o myprogram myprogram.o -L$HOME/PnetCDF/1.12.0/lib -lpnetcdf

or for Fortran

/usr/bin/mpifort -O2 -o myprogram myprogram.o -L$HOME/PnetCDF/1.12.0/lib -lpnetcdf

Running Your User Program

If PnetCDF is configured with –enable-shared option, then one may need to add the installation path of PnetCDF to the environment variable before running. For bash shell,

export LD_LIBRARY_PATH=$HOME/PnetCDF/1.12.0/lib

For csh or tcsh shell,

setenv LD_LIBRARY_PATH $HOME/PnetCDF/1.12.0/lib

If PnetCDF is configured and built with static library only (default), then one can skip the above setting for environment variable LD_LIBRARY_PATH.

An example command to run the user program in parallel using 4 MPI processes is shown below.

/usr/bin/mpiexec -n 4 ./myprogram

Previous: , Up: Use of the PnetCDF Library   [Index]