Chapter 1#

First Fortran MUSICA Program#

The MUSICA-Fortran API provides access to the MUSICA library within a fortran program. To get started, let us create a simple program that links to MUSICA and prints the version of MICM.

Save the following code to a file named demo.F90:

program demo

  use musica_util, only: string_t
  use musica_micm, only: get_micm_version
  implicit none

  type(string_t) :: micm_version

  micm_version = get_micm_version()
  print *, "MICM version ", micm_version%get_char_array()

end program demo

From the musica_micm module, we only need the function get_micm_version, which returns a derived string type from the musica_util module, string_t. (The string_t type will be discussed in more detail in later chapters.) To print the version string we just want the fortran character array, accessed by the get_char_array function.

Now, to build this simple program, invoke the gfortran compiler and link to libmusica-fortran, libmusica, yaml-cpp, and the standard C++ library libstdc++. The full command is

gfortran -o demo demo.F90 -I<MUSICA_DIR>/include -L<MUSICA_DIR>/lib64 -lmusica-fortran -lmusica -lstdc++ -lyaml-cpp

<MUSICA_DIR> is the full path of the MUSICA installation directory, specified by the option CMAKE_INSTALL_PREFIX during installation (see Installing MUSICA). Note that the include path allows the linker to find the musica_micm.mod and musica_util.mod module definition files.

When the demo program is run it should display the MICM version:

$ ./demo
 MICM version 3.6.0
$

Building a MUSICA Fortran Program with CMake#

A minimal CMakeLists.txt file designed to link the musica_fortran library to the demo_f.F90 file described above is exhibited below

cmake_minimum_required(VERSION 3.21)

project(
  musica-demo
  VERSION 0.1
  LANGUAGES CXX C Fortran
)

set(MUSICA_INCLUDE_DIR "${MUSICA_INSTALL_DIR}/include")
set(MUSICA_LIB_DIR "${MUSICA_INSTALL_DIR}/lib64")

message(STATUS "${MUSICA_INCLUDE_DIR}")
message(STATUS "${MUSICA_LIB_DIR}")

add_executable(demo_f demo.F90)

target_include_directories(demo_f PUBLIC ${MUSICA_INCLUDE_DIR})
target_link_directories(demo_f PUBLIC ${MUSICA_LIB_DIR})
target_link_libraries(demo_f musica-fortran musica stdc++ yaml-cpp)

Common practice is to create a build subdirectory (relative to the top level CMakeLists.txt file shown above).

mkdir build
cd build

Then, cmake can then be invoked with:

cmake -DMUSICA_INSTALL_DIR=<MUSICA_DIR> ..
make

Then, the demo_f executable can be run:

$ ./demo_f
  MICM version 3.6.0
$