f_and_c/README.md
2024-05-15 10:18:26 +08:00

2.3 KiB

call fortran routine from C++ function

  • a FORTRAN90 subroutine is like a C++ void function, and should be declared this way in the C++ code. The qualifier extern should also be used.
  • a FORTRAN90 subroutine or function expects all its arguments to be passed by reference. This generally means simply that the C++ function must pass scalar variables by reference, not value.
  • typically, when the FORTRAN90 compiler compiles the FORTRAN90 code, the names of functions and subroutines are stored with an appended underscore. In order for these names to be found by the C++ code, it is necessary that the C++ code declare and invoke the FORTRAN90 functions and subroutines with the underscore explicitly appended to the name.
  • in many cases, a FORTRAN90 compiler is simply a "front end" to a corresponding C++ compiler, as in the case of the GNU compilers gfortran and g++, or the Intel compilers ifort and icpp. This means that, as long as the corresponding compilers are used to compile the FORTRAN90 and C++ codes, it is probably possible to use either compiler to link and load the object codes; however, the load command may need to specify explicitly certain libraries associated with one of the languages. For instance, if loading using the gcc command, it is necessary to include "-l gfortran" so that the FORTRAN90 I/O libraries, among others, are included in the build.

use bind(C) in F90


gfortran -shared -o math_module.so math_module.f90
g++ hello.cpp -L. -lmath_module -o main
g++ hello.cpp /path/to/math_module.so -o main

install mini-requirements for clm

netCDF-C

sudo apt install libhdf5-dev
wget https://path/to/netcdf-c.source.zip
unzip netCDF.zip
cd netcdf
mkdir build
cd build
cmake ../.
make
sudo make install

netcdf-fortran

wget https://github.com/Unidata/netcdf-fortran/archive/refs/tags/v4.6.1.zip
unzip netcdf-fortran
cd netcdf-fortran
./configure
make check
sudo make install

pnetcdf


git clone https://github.com/Parallel-NetCDF/PnetCDF.git
cd PnetCDF
autoreconf -i

./configure
make
sudo make install

lapack

wget https://github.com/Reference-LAPACK/lapack/archive/refs/tags/v3.12.0.zip
unzip v3.12.0.zip
cd v3.12.0
mkdir build
cd build
cmake ..
make
sudo make install