Library and Header Paths on HPC
Many codes need headers (C/C++) or modules (Fortran) for procedure prototypes, etc. This is especially common if it is using an external library. As was true for libraries, there is a system default search path for these “include files.”
The compiler flag is -I/path/to/includes
The path to Fortran module files is also specified by the -I flag.
The lmod modules system we use is hierarchical. You must first load your choice of compiler. From there you can type
module avail
The modules at the top will be for libraries built with that compiler.
You will generally need to provide paths for these libraries by -L and -I
flags. You can find the paths with
printenv | grep ROOT
Most recent modules have an environment variable NAME_ROOT, e.g.
HDF5_ROOT
Example
A number of scientific and engineering codes need the input/output libraries HDF5 or NetCDF. We will use NetCDF for this example.
module load gcc
module load netcdf
printenv | grep ROOT
The following should be typed on a single line:
gfortran -o myexec -L${NETCDF_ROOT}/lib -I${NETCDF_ROOT}/include mycode.f90 -lnetcdff -lnetcdf
Exercises
Exercise 1. Multiple files (no C example here)
Copy from /share/resources/tutorials/compilers your choice of files
- Copy
main.cxxormain.f90 - Copy
sub1.cxxorsub1.f90 - Copy
sub2.cxxorsub2.f90 - C++ copy
sub1.handsub2.has well
Use these three files to create an executable.
Exercise 2. External Libraries
-
Load the netcdf module for your choice of compiler suite as indicated above.
-
Copy
simple_xy_wr.corsimple_xy_wr.cpporsimple_xy_wr.f90(note the C++ suffix iscpphere). -
Compile and link the examples. You will need to add
-I${NETCDF_ROOT}/includeand-L${NETCDF_ROOT}/libto your link command. For C use-lnetcdffor the library. For Fortran use-lnetcdff -lnetcdfin that order. For C++ use-lnetcdf_c++ -lnetcdfin that order.