1.3. CMake Files in MeVisLab

CMake based projects generally contain a file CMakeLists.txt at root level that describes the steps to build, test, and install a project.

In MeVisLab, there are some special CMake functions that handle scenarios that are specific to MeVisLab and its package structure.

Side note: CMakeLists.txt can also include the CMakeLists.txt of sub-directories with the command add_subdirectory().

A typical example (MLModuleWrapperExample/CMakeLists.txt):


cmake_minimum_required(VERSION 3.20)
project(MLModuleWrapperExample)

find_package(MeVisLab COMPONENTS ML MLABBase Network HINTS "$ENV{MLAB_ROOT}" REQUIRED)

set(CMAKE_AUTOMOC TRUE)
find_package(Qt6 COMPONENTS Core Gui REQUIRED)

add_library(MLModuleWrapperExample "")
add_library(MeVisLab::MLModuleWrapperExample ALIAS MLModuleWrapperExample)

generate_export_header(MLModuleWrapperExample)

target_sources(MLModuleWrapperExample PRIVATE
    mlModuleWrapperExample.cpp
    mlModuleWrapperExample.h
    MLModuleWrapperExampleInit.cpp
    MLModuleWrapperExampleInit.h
)

target_compile_definitions(MLModuleWrapperExample PRIVATE 
    MEVIS_TARGET=MLModuleWrapperExample)

target_link_libraries(MLModuleWrapperExample
  PUBLIC
    Qt6::Core
    Qt6::Gui
    MeVisLab::ML
    MeVisLab::Network
    MeVisLab::MLABBase
)

mlab_install(MLModuleWrapperExample NS MeVisLab)
      

Here is a short explanation of the individual statements:


cmake_minimum_required(VERSION 3.20)
      

This tells CMake which CMake version is required at minimum. This is important, because it also activates/deactivates some features in the CMake language, depending on the given version. MeVisLab currently requires 3.20.


project(MLModuleWrapperExample)
      

This sets the name of this project. However, this is the simplest form. It is recommended to specify at least the VERSION.


find_package(MeVisLab COMPONENTS ML MLABBase Network HINTS "$ENV{MLAB_ROOT}" REQUIRED)
      

This command tells CMake that the project links against parts of MeVisLab. The actual 'parts' are listed after the COMPONENTS option.

The HINTS option tells CMake where to find the central MeVisLab cmake file, which resolves the linked libraries and adds the required additional CMake functions. In this case it tells CMake to search relative to the MLAB_ROOT environment variable, which should point to the packages directory of your MeVisLab SDK installation directory. (This should automatically be set by the SDK installer.)

[Note] Note

find_package(MeVisLab) not only finds MeVisLab, it also loads additional functions and configures various CMake settings. It is important to execute this call as early as possible in the project, especially before other find_package calls are made or custom CMake settings are applied.


set(CMAKE_AUTOMOC TRUE)
      

This is a project that builds against the Qt toolkit. Setting the variable CMAKE_AUTOMOC to TRUE tells CMake that the Qt moc tool should automatically be applied to all relevant source files.


find_package(Qt6 COMPONENTS Core Gui REQUIRED)
      

This tells CMake to look for Qt6 in the third-party libraries, and the necessary components. Note that this should always come after find_package(MeVisLab), as this sets some CMake search paths; otherwise another Qt installed on your system might be found.

There should be a find_package() statement for every required third-party dependency.


add_library(MLModuleWrapperExample "")
add_library(MeVisLab::MLModuleWrapperExample ALIAS MLModuleWrapperExample)
      

This tells CMake to add a library target called MLModuleWrapperExample.

It is good practice to add the target under a vendor-specific namespace, which is done with the second add_library. A different namespace than MeVisLab shall be used for own projects to avoid name conflicts. Always link to projects with their namespace name.


generate_export_header(MLModuleWrapperExample)
      

It generates and uses a header file that contains platform and compiler specific EXPORT macros.


target_sources(MLModuleWrapperExample PRIVATE
    mlModuleWrapperExample.cpp
    mlModuleWrapperExample.h
    MLModuleWrapperExampleInit.cpp
    MLModuleWrapperExampleInit.h
)
      

This lists the source files that should be compiled. Note that these files should usually be defined as PRIVATE.


target_compile_definitions(MLModuleWrapperExample PRIVATE 
    MEVIS_TARGET=MLModuleWrapperExample)
      

Set the MEVIS_TARGET compile definition to be used for the compile process.


target_link_libraries(MLModuleWrapperExample
  PUBLIC
    Qt6::Core
    Qt6::Gui
    MeVisLab::ML
    MeVisLab::Network
    MeVisLab::MLABBase
)
      

These are the targets this project links against. Some belong to Qt, some belong to MeVisLab itself. Note that since the dependencies are public, they are also made part of the link interface and would be automatically available to projects linking this target.


mlab_install(MLModuleWrapperExample NS MeVisLab)
      

Generates MeVisLab specific install rules for this project.

One should use


mlab_install(MyProject NS MyNamespace)
      

for custom projects, that is, use a different namespace than MeVisLab to avoid name conflicts (it should correspond to the namespace in the second add_library statement). And if other projects should be able to link against this project, use these statements instead:


mlab_install(MyProject NS MyNamespace EXPORT)
mlab_install_headers(MyProject)