User Tools

Site Tools


CMake tutorial

CMake is a cross-platform build system with many nice features. It generates platform specific build files, such as Makefiles or Visual Studio project files during configuration.

It supports multiple targets, e.g. out-of-tree configurations and builds (simply configure from multiple build directories).


To add new files to be compiled, you will need to edit one of the following files


Add new files to new lines which makes it less likely to conflict when merging code in git.

set (oofemlib_element

For more complex additions that require new configuration options, you will need to modify CMakeLists.txt in the root directory


If new tests files are added CMake will find them automatically, just re-run cmake from the build directory.


Install CMake from the package system of your choice. Create an out-of-tree build directory:

mkdir -p ~/build/debug/
cd ~/build/debug/
cmake ~/oofem/

Instead of cmake you can use ccmake which uses an ncurses interface, or cmake-gui for a GUI. Use the command make help for a list of all targets.

To make configuration options, either use the cmake, cmake-gui or supply additional commands directly

cmake -DCMAKE_CXX_FLAGS="-Werror" ~/oofem/

In order to use a different compiler you need to start a new clean configuration (from an empty directory) and supply the new compiler using -DCMAKE_CXX_COMPILER

mkdir -p ~/build/clang/
cd ~/build/clang/
cmake -DCMAKE_CXX_COMPILER=clang++ ~/oofem/

Note: After initial configuration, you cannot change compiler. If you install a new version of GCC, you will need to remove the build directory and do the configuration again. Just doing make clean isn't enough.


* The build process usually hides the compiler command lines, to show them, use

 $ make VERBOSE=1


Some useful options for ctest include running the tests in parallel;

ctest -j 4

and regex search for limiting the tests you wish to check (the following example runs and

ctest -R cemhyd

and lastly a verbose option to see what went wrong

ctest -V


CPack allows you to make source and binary releases. You will first need to configure using cmake (as detailed above). The following commands are executed from the build directory.

To generate a source package (which excludes .git/ and generated files) run

make source_package

To generate a DEB, RPM, ZIP binary packages of your currently configured directory, run

cpack -G DEB
cpack -G RPM
cpack -G ZIP


The KDevelop IDE has build in support for CMake projects (and GIT). It includes a graphical configuration of multiple targets. Simply choose to import a project, and select CMakeLists.txt from the source directory You can then open the configuration from the menu and make your choices:

Python bindings

With CMake, it is also possible to create shared library loadable as Python module. You need CMake version at least 2.8.8 and two additional cmake parameters ( -DUSE_SHARED_LIB=ON and -DUSE_PYTHON_BINDINGS=ON ). After standard make procedure, file is created in build directory. From python it can be loaded and use like this:

import sys
sys.path.append("/path/to/build/directory") # path where was created
import liboofem

a = liboofem.FloatArray(2)
a[1] = 15.0
a[0] = 10.0
print a[0], a[1]

Windows (Visual Studio)

  1. Install CMake
  2. Start CMake, select the source directory, and then select a new directory for the build.
  3. Press configure, select your options, configure again
  4. Press generate to produce project files for VS.

To run the test suite, open the solution explorer in VS and “build” the tests or benchmarks targets to run through the test suite or benchmark suite respectively.

Note: The build types (debug, release) are handled internally by Visual Studio, so no configuration option for this is presented in CMake

Note: PETSc isn't easily built on windows, so support for parallel builds is still lacking

Windows (MinGW)

  1. Install CMake (tested with version 3.3.2)
  2. Install a new mingw-w64 (tested with version 5.2.0). It has support for 32-bit if necessary. MinGW-w64 version is selected from a GUI installer.
  3. Run $ mingw32-make.exe -v
  4. Run a batch e.g. C:\Program Files (x86)\mingw-w64\i686-5.2.0-posix-dwarf-rt_v4-rev1\mingw-w64.bat which opens a new terminal. In that terminal, we have already PATH to mingw32-make.exe. If the PATH is already set up, any terminal would work.
  5. Create directory, e.g. mkdir C:\oofem\optimized and cd under optimized.
  6. Run cmake with flags, e.g. $ cmake.exe -G“MinGW Makefiles” -DCMAKE_BUILD_TYPE=RELEASE -DUSE_SHARED_LIB=“OFF” -DUSE_IML=“ON” -DUSE_DSS=“ON” ..
  7. Run mingw32-make.exe -j 3 to compile
  8. If you want smooth transfer of oofem.exe to another windows computer, you need to put all *.dll from C:\Program Files (x86)\mingw-w64\i686-5.2.0-posix-dwarf-rt_v4-rev1\mingw32\bin in the same directory as oofem.exe.
  9. Run $ctest for checking OOFEM tests
cmake.txt · Last modified: 2017/01/25 17:34 by smilauer