Build an external application with CMake
Use this page when you have already built or installed IBAMR and want to compile your own application against it. The IBAMR CMake package provides two main targets:
IBAMR::IBAMR2dfor 2D applicationsIBAMR::IBAMR3dfor 3D applications
Link each executable to the target that matches its dimensionality. The target
supplies the IBAMR include directories, linked libraries, C++ standard
requirement, and dimensional compile definitions such as NDIM.
Complete minimal project
Create a project directory containing your application source file and a
CMakeLists.txt file:
my_ibamr_application/
|-- CMakeLists.txt
`-- main.cpp
The project enables both C and C++ because the IBAMR CMake package checks both MPI language components, even when the application source files are C++.
For a 2D application, use this complete CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(my_ibamr_application LANGUAGES C CXX)
find_package(IBAMR REQUIRED)
add_executable(main2d main.cpp)
target_link_libraries(main2d PRIVATE IBAMR::IBAMR2d)
For a 3D application, use this version instead:
cmake_minimum_required(VERSION 3.15)
project(my_ibamr_application LANGUAGES C CXX)
find_package(IBAMR REQUIRED)
add_executable(main3d main.cpp)
target_link_libraries(main3d PRIVATE IBAMR::IBAMR3d)
The main.cpp file is your application source file. If your application has
more source files, list them in the corresponding add_executable() command:
add_executable(main2d main.cpp mechanics_model.cpp fluid_model.cpp)
Header files such as mechanics_model.h or fluid_model.h usually do not need
to be listed there. They are included by the source files that use them.
In these examples:
find_package(IBAMR REQUIRED)locates the IBAMR CMake package.add_executable()creates the application executable from your source files.target_link_libraries()connects that executable to IBAMR.- The
IBAMR::prefix is part of the exported CMake target name; keep it in thetarget_link_libraries()call.
Most applications can use find_package(IBAMR REQUIRED). If your application
needs a specific IBAMR version, add a version argument:
find_package(IBAMR <version> REQUIRED)
Replace <version> with the required version number.
Configure and build
Configure the external application with the same compiler and MPI environment
you used for IBAMR. Run the configure command from the project directory
containing CMakeLists.txt; -S . tells CMake to use the current directory as
the source directory, and -B build tells CMake to write build files in a
separate build directory.
The $(which mpicc) and $(which mpicxx) substitutions use the MPI compiler
wrappers currently in your shell. If you loaded modules or changed environment
variables when building IBAMR, use the same environment here.
The Building page documents optimized and
debugging IBAMR builds. These examples use -DCMAKE_BUILD_TYPE=Release to match
an optimized IBAMR build. If you built IBAMR in debug mode, use
-DCMAKE_BUILD_TYPE=Debug here and point CMake to that debug IBAMR build or
installation.
If IBAMR has been installed, use the IBAMR installation prefix:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER="$(which mpicc)" \
-DCMAKE_CXX_COMPILER="$(which mpicxx)" \
-DCMAKE_PREFIX_PATH="/path/to/ibamr/install"
Replace /path/to/ibamr/install with the installation prefix used when IBAMR was
installed. CMAKE_PREFIX_PATH tells CMake where to search for installed
packages.
If IBAMR has been built but not installed, point CMake to the package configuration directory in the IBAMR build tree:
cmake -S . -B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER="$(which mpicc)" \
-DCMAKE_CXX_COMPILER="$(which mpicxx)" \
-DIBAMR_DIR="/path/to/ibamr/build/cmake"
After configuring, build the application:
cmake --build build -j
If CMake cannot find IBAMR, set IBAMR_DIR to the directory containing
IBAMRConfig.cmake. For an installed IBAMR, this is usually
/path/to/ibamr/install/lib/cmake/ibamr.