Skip to content

First program and example build layout

This section explains how tutorial examples are built as standalone units. It assumes your environment is already set up (see Installation, preferably via ./setup-env.sh).

Each directory under examples/ is treated as a small DUNE module:

  • it has a dune.module,
  • it has a CMakeLists.txt,
  • it contains one or more .cc files.

1. Minimal C++ program

Example source (examples/01_grid_creation/grid_creation.cc):

#include <array>
#include <iostream>

#include <dune/grid/yaspgrid.hh>

int main()
{
    constexpr int dim = 2;
    Dune::FieldVector<double, dim> L(1.0);
    std::array<int, dim> N{4,4};
    Dune::YaspGrid<dim> grid(L, N);

    auto gv = grid.leafGridView();
    std::cout << "Grid dimension: " << gv.dimension << "\n";
    std::cout << "Number of elements: " << gv.size(0) << "\n";
}

2. dune.module file

The module file declares dependencies between modules.

Example (examples/01_grid_creation/dune.module):

Module: dune-tutorial-example-01
Depends: dune-common dune-geometry dune-grid

Module is the module name used by dunecontrol for dependency resolution. Depends lists the DUNE modules required by this example. There are additional dune.module fields, but we ignore them here and return to them in advanced tutorials.

3. CMakeLists.txt file

Use regular CMake package discovery and imported DUNE targets.

Example (examples/01_grid_creation/CMakeLists.txt):

cmake_minimum_required(VERSION 3.16)
project(dune_tutorial_example_01 LANGUAGES CXX)

find_package(dune-common REQUIRED)
find_package(dune-geometry REQUIRED)
find_package(dune-grid REQUIRED)

add_executable(example01 grid_creation.cc)
target_link_libraries(example01 PRIVATE Dune::Common Dune::Geometry Dune::Grid)

For other examples, adjust:

  • target name,
  • source file names,
  • dependency list.

4. Build an example with dunecontrol

If you used this tutorial's setup script, activate once from repository root:

source _env/activate.sh

If you use your own DUNE source installation, ensure your shell has:

  • dunecontrol in PATH,
  • DUNE_CONTROL_PATH pointing to your module directory set.

Then build from the example directory:

cd examples/01_grid_creation
dunecontrol --current all

Then run:

./build/debug/example01

To build examples in release mode:

source _env/activate.sh --release

5. Why this layout?

  • Every example can be built independently.
  • Dependencies stay explicit and close to the code.
  • You can open each example directory directly in an IDE with compile commands available via CMake.

Summary and next steps

You now know the minimal structure for standalone DUNE example modules and how to build them with dunecontrol. Continue with Core modules and dune-grid: first contact to start the grid tutorial sequence.