Wiki

Clone wiki

lifev-release / tutorial / CMake

Go back


CMake and external applications

Internally, LifeV uses Tribits for its configuration and the generation of the Makefiles necessary for compiling the source code. Tribits is based on CMake and is the configuration tool used also by Trilinos. When adding an example or a test to LifeV, one has to use Tribits for the integration of the own source code inside the LifeV source tree.

Nevertheless, LifeV can be used also as an external library. In order to do this, an installation of LifeV is required. To install LifeV in a particular directory, configure LifeV with the additional argument:

    -DCMAKE_INSTALL_PREFIX:PATH=lifev-install-directory
and compile it by issuing the make command:
make -C lifev-build-directory -j <num_jobs> install
From lifev-build-directory it will compile the source code and install LifeV inside the specified directory lifev-install-directory.

LifeV can then be used from an external application. The following is a template CMakeLists.txt file which can be used as a base for configuring and compiling an external application using LifeV:

#!CMake
# you need CMake version >= 2.8 to use this example.
cmake_minimum_required( VERSION 2.8 )

# look for LifeV in the directory provided
find_package( LifeV PATHS ${LIFEV_PATH}/lib/cmake/LifeV ${LIFEV_PATH} )

# if the C++ compiler is not set, use the same compiler used for building LifeV
if ( NOT CMAKE_CXX_COMPILER )
    message( "Setting CXX compiler." )
    set( CMAKE_CXX_COMPILER ${LifeV_CXX_COMPILER} )
endif ()

# if the C compiler is not set, use the same compiler used for building LifeV
if ( NOT CMAKE_C_COMPILER )
    message( "Setting C compiler." )
    set( CMAKE_C_COMPILER ${LifeV_C_COMPILER} )
endif ()

# set a name for the project
project( LifeVExample )

# set the source files
set( SOURCES
    file1.cpp
    file2.cpp
)

set( HEADERS
    file1.hpp
    file2.hpp
)

# force C++11
add_definitions( -std=c++11 )

# add the include paths
include_directories( "." )
include_directories( ${LifeV_TPL_INCLUDE_DIRS} )
include_directories( ${LifeV_INCLUDE_DIRS} )

# add the link paths
link_directories( ${LifeV_LIBRARY_DIRS} )
link_directories( ${LifeV_TPL_LIBRARY_DIRS} )

# set the name of the executable
set( EXE lifev_example )

# add the sources to compile for building the executable
add_executable( ${EXE} ${SOURCES} ${HEADERS} )

# set the libraries to link against
target_link_libraries( ${EXE}
    ${LifeV_LIBRARIES}
    ${LifeV_TPL_LIBRARIES}
)

The application can be configured, for example, by creating the following do_configure.sh script:

#!/bin/bash

mkdir -p build
cd build

cmake -V \
  -D LIFEV_PATH:FILEPATH="lifev-install-directory" \
  -D CMAKE_BUILD_TYPE:STRING=RELEASE \
  -D BUILD_SHARED_LIBS:BOOL=ON \
  -D CMAKE_VERBOSE_MAKEFILE:BOOL=OFF \
  ..
and executing it by issuing the command ./do_configure.sh. The script will create the directory build and configure the application inside that directory. Then, the application can be compiled by issuing the command make -C build -j <num_jobs> and executed with build/lifev_example.

Updated