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. With more recent
8 versions, the Fortran netCDF library has been split off into an
9 independent source distribution, intended to be built as a separate
10 library, *after* the C library is built and installed. This separation
11 simplifies the building and use of the C and Fortran netCDF libraries
12 and allows them to evolve independently.
14 More recently, building netcdf-fortran as an automated step in the
15 netcdf-c build is now possible (for non-MSVC builds) by using new
16 options at configure time, so if you haven't built and installed the
17 netCDF-C library yet, you may want to try the new netCDF-C <a
18 href="http://www.unidata.ucar.edu/software/netcdf/docs/getting_and_building_netcdf.html#getting"
19 >Fortran-bootstrap procedure</a>.
21 In the example commands below, we assume use of a
22 POSIX-standard shell, such as sh, bash, ksh, or zsh. If you are using
23 csh instead, you will have to use the
25 setenv ENV_VARIABLE value
27 syntax to set environment variables instead of the
31 syntax used in the examples. In either case, `${VAR}` is the
32 value of the shell variable or environment variable VAR.
34 It will be easier to build the netCDF Fortran library if the C (and if
35 needed, HDF5) libraries are built as shared libraries (the default), but
36 you can also use static libraries, as described in a later section.
38 Building with shared libraries {#building_fortran_shared_libraries}
39 ==============================
41 1. First make sure the netCDF C library has been built, tested, and
42 installed. The shell variable NCDIR should be set such that
43 the shared library for netCDF C is under `${NCDIR}/lib` and
44 netCDF utilities such as ncdump are under `${NCDIR}/bin`. For example:
48 2. The configure script will try to determine suitable Fortran and C
49 compilers for building netCDF Fortran, but you can instead specify
50 them with the FC and CC environment variables, if needed. For
56 3. If the netCDF C library was installed as a shared library in a
57 location that is not searched by default, you will need to set the
58 LD\_LIBRARY\_PATH environment variable (or DYLD\_LIBRARY\_PATH on
59 OSX) to specify that directory before running the configure script.
62 export LD_LIBRARY_PATH=${NCDIR}/lib:${LD_LIBRARY_PATH}
64 4. If you set the LD\_LIBRARY\_PATH (or DYLD\_LIBRARY\_PATH)
65 environment variable in the previous step, don't use "sudo"
66 before the following "configure" or "make check" commands.
67 Using "sudo" causes the LD\_\* environment variables to be
68 ignored. You can use "sudo make install" as the last
69 step if necessary, but don't use "sudo" before that.
71 5. Set the shell variable `NFDIR` to where you want to install
72 netCDF Fortran, which can be the same location where the netCDF C
73 was installed (default /usr/local), but **not** the source
74 directory where you are building the software. Then, from the top-level
75 source directory, run the configure script, using CPPFLAGS and LDFLAGS to
76 specify where the netCDF C library was installed:
79 CPPFLAGS=-I${NCDIR}/include LDFLAGS=-L${NCDIR}/lib \
80 ./configure --prefix=${NFDIR}
82 If you are cross-compiling, you should also include the configure
83 option "--disable-fortran-type-check", as in:
85 CPPFLAGS=-I${NCDIR}/include LDFLAGS=-L${NCDIR}/lib \
86 ./configure --prefix=${NFDIR} --disable-fortran-type-check
88 To see other configure options, run `configure --help`.
90 6. If that succeeds, run
94 7. If that succeeds, run
102 If you encounter problems, send the complete "config.log" file
103 generated by running configure to support-netcdf@unidata.ucar.edu.
105 Building with static libraries {#building_fortran_with_static_libraries}
106 ==============================
108 If you can't build the C netCDF library as a shared library or if it has
109 already been installed by someone else only as a static library (which
110 means there are no libnetcdf.so files in the library directory where the
111 netCDF C library was installed), then building and installing the
112 Fortran netCDF libraries will be more complicated.
114 1. The configure script will try to determine suitable Fortran and C
115 compilers for building netCDF Fortran, but you can also, specify
116 them with the FC and CC environment variables.
118 2. Assume the static netCDF C library is installed under `${NCDIR}`,
119 the HDF5 library under `${H5DIR}`, and other needed libraries
120 such as zlib and curl under `${ODIR}`. Some or all of these could
121 be the same (for example /usr/local).
123 3. Let the shell variable `${NFDIR}` specify where you want to
124 install the netCDF Fortran library. This can be the same location
125 where the netCDF C library is installed (default is /usr/local).
127 4. Set the CPPFLAGS, LDFLAGS, LD\_LIBRARY\_PATH, and LIBS environment
128 variables to specify where the netCDF C library is installed and
129 where the other libraries may be found. For example:
131 CPPFLAGS="-I${NCDIR}/include -I${H5DIR}/include -I${ODIR}/include" \
132 LDFLAGS="-L${NCDIR}/lib -L${H5DIR}/lib -L${ODIR}/lib" \
133 LD_LIBRARY_PATH=${NCDIR}/lib:${H5DIR}/lib:${ODIR}/lib \
134 LIBS="-lnetcdf -lhdf5_hl -lhdf5 -lz -lcurl" \
135 ./configure --disable-shared --prefix=${NCDIR}
137 If you are cross-compiling, you should also include the configure
138 option "--disable-fortran-type-check".
140 5. For parallel I/O: The configure script sets CFLAGS appropriately for
141 standard compilers, but if you are building with parallel I/O using
142 wrappers such as mpicc, mpif90, and mpif77, specify compilers
143 using the CC, FC, and F77 variables before configure. For example:
145 CC=mpicc FC=mpif90 F77=mpif77 CPPFLAGS=-I${NCDIR}/include \
146 LDFLAGS=-L${NCDIR}/lib ./configure --prefix=${NFDIR}
148 You may have to use absolute path names for CC, F90, and F77 if
149 configure can't find them. Finally, you may also need to set
150 CFLAGS to indicate which Fortran compiler is wrapped by mpif90 nd
151 mpif77. For example, if "mpif90 --show" indicates gfortran is
152 being used, then set CFLAGS=-DgFortran, and similarly set
153 CFLAGS=-DpgiFortran for Portland Group compilers.
155 6. If that succeeds, run "make check".
157 7. If that succeeds, run "make install" or "sudo make install".
159 If you encounter problems, send the complete "config.log" file
160 generated by running configure to support-netcdf@unidata.ucar.edu.
162 Linking your programs with netCDF Fortran libraries {#linking_against_netcdf_fortran}
163 ==============================
165 If you built the shared libraries, you can link with something like
167 fortran my_prog.f -o my_prog -I${NFDIR}/include -L${NFDIR}/lib -lnetcdff
169 to link your Fortran software with the installed netCDF Fortran and C
172 If you didn't install the shared libraries in a standard place, you
173 may need to set LD\_LIBRARY\_PATH (or DYLD\_LIBRARY\_PATH for OSX) to
174 include `${NFDIR}/lib` before running the resulting
175 program. Alternatively, you may add `${NFDIR}/lib` to the
176 LD\_RUN\_PATH environment variable before linking, or use the
177 `-Wl,-rpath -Wl,${NFDIR}/lib` linker flag, or have your system
178 administrator add `${NFDIR}/lib` to `/etc/ld.so.conf'. See
179 operating system documentation about shared libraries for more
180 information, such as the ld(1) and ld.so(8) manual pages.
182 If you built static libraries, you will need to use something like
184 fortran my_prog.f -o my_prog -I${NFDIR}/include \
185 -L${NCDIR}/lib -lnetcdff -lnetcdf \
186 -L${H5DIR}/lib -lhdf5_hl -lhdf5 -L${ODIR} -lz -lcurl -lm
188 to link Fortran software with the installed Fortran library and the
189 libraries on which it depends.
191 A simpler alternative that should work for either shared or static
192 libraries is to use the "nf-config" utility installed in `${NFDIR}/bin`:
194 fortran my_prog.f -o my_prog `nf-config --fflags --flibs`
196 or the more general "pkg-config" utility, if you have it:
198 fortran my_prog.f -o my_prog `pkg-config --cflags --libs netcdf-fortran`
200 Specifying The Environment for Building {#specify_build_env_fortran}
201 ========================================
203 For cross-compiles, the following environment variables can be used to override the default fortran/C type settings like this (in sh):
205 export NCBYTE_T="integer(selected_int_kind(2))"
206 export NCSHORT_T="integer*2"
207 export NF_INT1_T="integer(selected_int_kind(2))"
208 export NF_INT2_T="integer*2"
209 export NF_INT1_IS_C_SHORT=1
210 export NF_INT2_IS_C_SHORT=1
211 export NF_INT_IS_C_INT=1
212 export NF_REAL_IS_C_FLOAT=1
213 export NF_DOUBLEPRECISION_IS_C_DOUBLE=1
215 In this case you will need to run configure with `–disable-fortran-compiler-check` and `–disable-fortran-type-check`.
217 Environment Variable Description Notes
218 --------------------------
220 Variable | Usage | Description
222 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.
223 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. Use –disable-f90 to disable the netCDF Fortran 90 API, but build the netCDF Fortran 77 API.
224 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 is not supported and may not work.
225 CFLAGS | C compiler flags | "-O -g2", for example.
226 CPPFLAGS | C preprocessor options | "-DNDEBUG" to omit assertion checks, for example.
227 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.
228 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.
229 ARFLAGS, NMFLAGS, FPP, M4FLAGS, LIBS, FLIBS, FLDFLAGS | Miscellaneous | One or more of these may be needed for some platforms. Unless required, you should not set these environment variables, because that may interfere with the configure script.