1 Building the NetCDF-4.2 and later Fortran libraries {#building_netcdf_fortran}
2 ===================================================
6 In versions before 4.2, the Fortran netCDF library source was bundled
7 with the C library source in one distribution, and it was possible to
8 combine the libraries in a single library file. With version 4.2, the
9 Fortran netCDF library for Fortran77 and Fortran90 APIs has been
10 separated into its own source distribution, and should now be built as a
11 separate library, after the C library is built and installed. This
12 separation simplifies the building and use of the C and Fortran netCDF
13 libraries and allows them to evolve independently.
15 Please note that in the example commands below, we assume use of a
16 POSIX-standard shell, such as sh, bash, ksh, or zsh. If you are using
17 csh instead, you will have to use the
19 setenv ENV_VARIABLE value
21 syntax to set environment variables instead of the
25 syntax used in the examples that use a POSIX-standard shell. In either
26 case, <I>${DIR1}</I> is the value of the environment variable <I>DIR1</I>.
28 It will be easier to build the netCDF Fortran library if the C (and if
29 needed, HDF5) libraries are built as shared libraries (the default), but
30 you can also use static libraries, as described in a later section.
32 Building with shared libraries {#building_fortran_shared_libraries}
33 ==============================
35 1. First make sure the netCDF C library has been built, tested, and
36 installed under directory <I>${DIR1}</I>, as specified by
37 --prefix=<I>${DIR1}</I> to the C library configure script, or under
38 directory /usr/local by default.
39 2. For the Fortran netCDF library, use the same C compiler as used to
40 create the netCDF C library, specified with the CC environment
41 variable, if necessary.
42 3. If the netCDF C library was installed as a shared library in a
43 location that is not searched by default, you will need to set the
44 LD\_LIBRARY\_PATH environment variable (or DYLD\_LIBRARY\_PATH on
45 OSX) to specify that directory before running the configure script,
48 export LD_LIBRARY_PATH=${DIR1}/lib:${LD_LIBRARY_PATH}
50 4. If you set the LD\_LIBRARY\_PATH (or DYLD\_LIBRARY\_PATH)
51 environment variable in the previous step, don't use the "sudo"
52 command before the following "configure" or "make check" commands.
53 Using "sudo" causes the LD\_\* environment variables to be ignored,
54 as a security precaution. You can use "sudo make install" as the
55 last step, but you shouldn't need to use "sudo" before that.
56 5. For the configure script, set CPPFLAGS and LDFLAGS variables to
57 specify the include and lib directories for the netCDF C library.
58 For example, to install the Fortran libraries in the same directory
59 *\${DIR1}* where the C netCDF library is installed:
61 CPPFLAGS=-I${DIR1}/include LDFLAGS=-L${DIR1}/lib ./configure --prefix=${DIR1}
63 If you are cross-compiling, you should also include the configure
64 option "--disable-fortran-type-check", as in:
66 CPPFLAGS=-I${DIR1}/include LDFLAGS=-L${DIR1}/lib \
67 ./configure --disable-fortran-type-check --prefix=${DIR1}
69 6. If that succeeds, run "make check".
70 7. If that succeeds, run "make install" or "sudo make install".
72 Building with static libraries {#building_fortran_with_static_libraries}
73 ==============================
75 If you can't build the C netCDF library as a shared library or if it has
76 already been installed by someone else only as a static library (which
77 means there are no libnetcdf.so files in the library directory where the
78 netCDF C library was installed), then building and installing the
79 Fortran netCDF libraries will be somewhat more complicated.
81 If you need to set the LD\_LIBRARY\_PATH (or DYLD\_LIBRARY\_PATH)
82 environment variable, don't use the "sudo" command before the following
83 "configure" or "make check" commands. Using "sudo" causes the LD\_\*
84 environment variables to be ignored. You can use "sudo make install" as
85 the last step, but you shouldn't need to use "sudo" before that.
87 1. Assume the static netCDF C library is installed under *\${DIR1}*,
88 and the other needed shared libraries for HDF5, zlib, and curl are
89 installed under *\${DIR2}* (which might be the same as *\${DIR1}*).
90 2. Use the same C compiler as used to create the netCDF C library,
91 specified with the CC environment variable, if necessary.
92 3. Set the CPPFLAGS, LDFLAGS, and LD\_LIBRARY\_PATH environment
93 variables to specify where the netCDF C library is installed and
94 where the other shared libraries may be found, before running the
95 configure script. For example:
97 CPPFLAGS="-I${DIR1}/include -I${DIR2}/include" \
98 LD_LIBRARY_PATH=${DIR1}/lib:${DIR2}/lib:${LD_LIBRARY_PATH} \
99 LDFLAGS="-L${DIR1}/lib -L${DIR2}/lib" \
100 LIBS="-lnetcdf -lhdf5_hl -lhdf5 -lz -lcurl" \
101 ./configure --disable-shared --prefix=${DIR1}
103 If you are cross-compiling, you should also include the configure
104 option "--disable-fortran-type-check".
106 4. For parallel I/O: The configure script sets CFLAGS appropriately for
107 standard compilers, but if you are building with parallel I/O using
108 wrappers such as mpicc and mpif90, you sometimes have to set CFLAGS
109 to indicate which Fortran compiler is wrapped by mpif90. For
110 example, if "mpicc --show" and "mpif90 --show" indicate gcc and
111 gfortran are being used, then set CFLAGS=-DgFortran, and similarly
112 set CFLAGS=-DpgiFortran for Portland Group compilers.
113 5. If that succeeds, run "make check".
114 6. If that succeeds, run "make install" or "sudo make install".
116 Linking your programs with netCDF Fortran libraries {#linking_against_netcdf_fortran}
117 ==============================
119 If you built the shared libraries, you can link with something like
121 fortran_compiler my_prog.f -o my_prog -I${DIR1}/include -L${DIR1}/lib -lnetcdff
123 to link your Fortran software with the installed netCDF Fortran and C
124 libraries. If you didn't install the shared libraries in a standard
125 place, you may need to set LD\_LIBRARY\_PATH (or DYLD\_LIBRARY\_PATH for
126 OSX) before running the resulting program.
128 If you built static libraries, you will need to use something like
130 fortran_compiler my_prog.f -o my_prog -I${DIR1}/include \
131 -L${DIR1}/lib -lnetcdff -lnetcdf -L${DIR2}/lib -lhdf5_hl -lhdf5 -lz -lcurl -lm
133 to link Fortran software with the installed Fortran library and the
134 libraries on which it depends.
136 A simpler alternative that should work for either shared or static
137 libraries is to use the "nf-config" utility installed in *${DIR1}*/bin:
139 `nf-config --fc` my_prog.f -o my_prog `nf-config --fflags --flibs`
141 or the more general "pkg-config" utility, if you have it:
143 fortran_compiler my_prog.f -o my_prog `pkg-config --cflags --libs netcdf-fortran`
146 Specifying The Environment for Building {#specify_build_env_fortran}
147 ========================================
150 The netCDF configure script searches your path to find the compilers and tools it needed. To use compilers that can't be found in your path, set their environment variables.
152 The configure script will use gcc and associated GNU tools if they are found. Many users, especially those with performance concerns, will wish to use a vendor supplied compiler.
154 For example, on an AIX system, users may wish to use xlc (the AIX compiler) in one of its many flavors. Set environment variables before the build to achieve this.
156 For example, to change the C compiler, set CC to xlc (in sh: export CC=xlc). (But don't forget to also set CXX to xlC, or else configure will try to use g++, the GNU C++ compiler to build the netCDF C++ API. Similarly set FC to xlf90 so that the Fortran APIs are built properly.)
158 By default, the netCDF library is built with assertions turned on. If you wish to turn off assertions, set CPPFLAGS to -DNDEBUG (csh ex: setenv CPPFLAGS -DNDEBUG).
160 If GNU compilers are used, the configure script sets CPPFLAGS to “-g -O2”. If this is not desired, set CPPFLAGS to nothing, or to whatever other value you wish to use, before running configure.
162 For cross-compiles, the following environment variables can be used to override the default fortran/C type settings like this (in sh):
164 export NCBYTE_T=''integer(selected_int_kind(2))''
165 export NCSHORT_T=''integer*2''
166 export NF_INT1_T=''integer(selected_int_kind(2))''
167 export NF_INT2_T=''integer*2''
168 export NF_INT1_IS_C_SHORT=1
169 export NF_INT2_IS_C_SHORT=1
170 export NF_INT_IS_C_INT=1
171 export NF_REAL_IS_C_FLOAT=1
172 export NF_DOUBLEPRECISION_IS_C_DOUBLE=1
174 In this case you will need to run configure with –disable-fortran-compiler-check and –disable-fortran-type-check.
176 Variable Description Notes
177 --------------------------
179 Variable | Usage | Description
181 CC | C compiler | If you don't specify this, the configure script will try to find a suitable C compiler. The default choice is gcc. If you wish to use a vendor compiler you must set CC to that compiler, and set other environment variables (as described below) to appropriate settings.
182 FC | Fortran compiler (if any)| If you don't specify this, the configure script will try to find a suitable Fortran and Fortran 77 compiler. Set FC to "" explicitly, or provide the –disable-f77 option to configure, if no Fortran interface (neither F90 nor F77) is desired. Use –disable-f90 to disable the netCDF Fortran 90 API, but build the netCDF Fortran 77 API.
183 F77 | Fortran 77 compiler (if any) | Only specify this if your platform explicitly needs a different Fortran 77 compiler. Otherwise use FC to specify the Fortran compiler. If you don't specify this, the configure script will try to find a suitable Fortran compiler. For vendor compilers, make sure you're using the same vendor's Fortran 90 compiler. Using Fortran compilers from different vendors, or mixing vendor compilers with g77, the GNU F77 compiler, is not supported and may not work.
184 CXX | C++ compiler | If you don't specify this, the configure script will try to find a suitable C++ compiler. Set CXX to "" explicitly, or use the –disable-cxx configure option, if no C++ interface is desired. If using a vendor C++ compiler, use that vendor's C compiler to compile the C interface. Using different vendor compilers for C and C++ may not work.
185 CFLAGS | C compiler flags | "-O" or "-g", for example.
186 CPPFLAGS | C preprocessor options | "-DNDEBUG" to omit assertion checks, for example.
187 FCFLAGS| Fortran 90 compiler flags | "-O" or "-g", for example. These flags will be used for FORTRAN 90. If setting these you may also need to set FFLAGS for the FORTRAN 77 test programs.
188 FFLAGS | Fortran 77 compiler flags | "-O" or "-g", for example. If you need to pass the same arguments to the FORTRAN 90 build, also set FCFLAGS.
189 CXXFLAGS | C++ compiler flags | "-O" or "-g", for example.
190 ARFLAGS, NMFLAGS, FPP, M4FLAGS, LIBS, FLIBS, FLDFLAGS | Miscellaneous | One or more of these were needed for some platforms, as specified below. Unless specified, you should not set these environment variables, because that may interfere with the configure script.