2

I knew this is old question, but I tried to search over StackOverFlow and did not find any good answer suitable for me. So I tried to repost this.

I am using linux and tried to build code using CMake and Make commands.

I tried to make separate my own code like http://docs.mitk.org/nightly/BuildInstructionsPage.html. I use this CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR) 
project(MyProject) 
find_package(MITK 2018.04.02 REQUIRED) 
add_executable(MyApp main.cpp) 
target_link_libraries(MyApp MitkCore)

But the find_package throws errors:

Could not find a package configuration file provided by "MITK" with any of the following names:

MITKConfig.cmake
mitk-config.cmake

As I read from cmake tutorial, it should be a cmake folder which has "MITKConfig.cmake" file and the CMakeLists should include that folder. But I dont find how to do that. Any suggestion ?

P/S: Using boot library is a good example of including external lib in our own project. Does anyone know how to do it with MITK libray ?

Phong Tran
  • 21
  • 2
  • You need to tell CMake where to find your library, if not installed in its default path. Look [here](https://stackoverflow.com/questions/20746936/what-use-is-find-package-if-you-need-to-specify-cmake-module-path-anyway) for example on how. This question has indeed been asked many times before for many different libraries and always the same answer. – nada Sep 30 '19 at 08:22
  • Thanks for your response. Yes I already tell CMake to find the library by adding find_package. But it seems like the library does not expose the *.cmake files. What I need here is an example of building a separate app which using the MITK library. Appreciate anyone who already did that. – Phong Tran Oct 01 '19 at 10:05
  • You have to follow rigorously the documentation about how to create an application based on Mitk. AFAIK Mitk does not ship as a library, and uses a superbuild system. This system tie your application with, at least, all the Mitk build tree. You should start using the template application (http://docs.mitk.org/nightly/HowToNewProject.html) – Arcadien Oct 18 '19 at 14:59
  • 1
    Thank you @Arcadien, yes I followed the instruction and can compile my application with MITK template. That's the only way to access the MITK superbuild – Phong Tran Oct 21 '19 at 04:49

1 Answers1

1

What I'm about show has been tested only with version 2018.04.99 on Pop!_OS 18.04.

The Step3 example can be compiled with this CMakeLists.txt:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(step3)

set(MITK_SUPERBUILD_DIR "/usr/mitk-build")
set(QT_DIR "/home/pop_os")

set(CMAKE_PREFIX_PATH
        "${MITK_SUPERBUILD_DIR}/MITK-build"
        "${MITK_SUPERBUILD_DIR}/ep/src/VTK/CMake/"
        "${QT_DIR}/Qt/5.12.8/gcc_64/lib/cmake/Qt5"
        )

find_package(MITK CONFIG REQUIRED)
find_package(OpenMP REQUIRED)

add_executable(${PROJECT_NAME} Step3.cpp)

target_link_libraries(${PROJECT_NAME} PUBLIC_HEADER
        MitkCore
        MitkQtWidgetsExt
        )

I'm developing an application which is more complex which is separated in modules (static libraries), I have a top level CMakeLists.txt which calls child sub-projects:

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(covid-gui-desktop LANGUAGES CXX)
    
# Qt compilations system variables.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON) # enables linker to qt moc headers
set (CMAKE_AUTOUIC ON) # enables linker to .ui files
set (CMAKE_AUTORCC ON) # enables linker to .qrc files

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_COLOR_MAKEFILE   ON)

set(MITK_SUPERBUILD_DIR "/usr/mitk-build")
set(QT_PATH "/home/pop_os")

set(CMAKE_PREFIX_PATH
        "${MITK_SUPERBUILD_DIR}/MITK-build/"
        "${MITK_SUPERBUILD_DIR}/ep/src/VTK/CMake/"
        "${QT_PATH}/Qt/5.12.8/gcc_64/lib/cmake/Qt5/"
        )

find_package(MITK CONFIG REQUIRED)
find_package(OpenMP REQUIRED)
find_package(Qt5 5.12 COMPONENTS Widgets Charts Svg REQUIRED)

### Static libraries
add_subdirectory(modules/AppDataManager)
add_subdirectory(modules/DarkStyle)
add_subdirectory(modules/VolumeVisualizationView)
add_subdirectory(modules/ThumbnailListView)
add_subdirectory(modules/DataManagerView)
###

set(cpp_files
        main.cpp
        MainWindow.cpp
        # Database / file managment
        Utils.cpp
        # Interface
        Concept1.cpp
        Concept2.cpp
        Concept3.cpp
        )

set(header_files
        MainWindow.h
        # Database / file managment
        Utils.h
        # Interface
        Concept1.h
        Concept2.h
        Concept3.h
        )

set(ui_files
        MainWindow.ui
        Concept1.ui
        Concept2.ui
        Concept3.ui
        )

set(qrc_files
        resource.qrc
        )

set(files ${cpp_files} ${header_files} ${ui_files} ${qrc_files})

add_executable(${PROJECT_NAME} ${files})

target_link_libraries(${PROJECT_NAME} PUBLIC
    MitkCore
    MitkQtWidgetsExt
    Qt5::Widgets
    Qt5::Charts
    Qt5::Svg
    AppDataManager
    DarkStyle
    VolumeVisualizationView
    DataManagerView
)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)

target_include_directories(${PROJECT_NAME} PUBLIC
        "${CMAKE_SOURCE_DIR}/modules/AppDataManager"
        "${CMAKE_SOURCE_DIR}/modules/DarkStyle"
        "${CMAKE_SOURCE_DIR}/modules/VolumeVisualizationView"
        "${CMAKE_SOURCE_DIR}/modules/DataManagerView"
        )

target_include_directories() allows the IDE to find static libraries .h and .cpp files. For MITK this is already done by find_package().

add_subdirectory() calls a CMakeLists.txt inside the static lib folder. Here is the one used for AppDataManager:

cmake_minimum_required(VERSION 3.17)

project(AppDataManager LANGUAGES CXX)

# Qt compilations system variables.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON) # enable linker to qt headers
set (CMAKE_AUTOUIC ON) #
set (CMAKE_AUTORCC ON) # enable link to qrc

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_COLOR_MAKEFILE   ON)

set(MITK_SUPERBUILD_DIR "/usr/mitk-build")
set(QT_PATH "/home/pop_os")

set(CMAKE_PREFIX_PATH
        "${MITK_SUPERBUILD_DIR}/MITK-build/"
        "${MITK_SUPERBUILD_DIR}/ep/src/VTK/CMake/"
        "${QT_PATH}/Qt/5.12.8/gcc_64/lib/cmake/Qt5/"
        )

find_package(MITK CONFIG REQUIRED)
find_package(OpenMP REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

set(cpp_files
        AppDataManager.cpp
        )

set(header_files
        AppDataManager.h
        )

add_library(${PROJECT_NAME} STATIC ${cpp_files} ${header_files})

target_compile_definitions(${PROJECT_NAME} PUBLIC cxx_std_14)

# Required on Unix OS family to be able to be linked into shared libraries.
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_link_libraries(${PROJECT_NAME} PUBLIC
        MitkCore
        MitkQtWidgetsExt
        Qt5::Widgets
        )

There may be a module (static lib) that depends on another module. AppDataManager is used by both the top-level application and by DataManagerView. If we use a CMakeLists.txt similar to the last one we can compile DataManagerView as a standalone lib successfully, but the top-level will fail due to the fact that we are calling add_subdirectory(modules/AppDataManager) twice, to avoid this I came up with this CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(DataManagerView LANGUAGES CXX)

# Qt compilations system variables.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON) # enable linker to qt headers
set (CMAKE_AUTOUIC ON) #
set (CMAKE_AUTORCC ON) # enable link to qrc

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_COLOR_MAKEFILE   ON)

set(MITK_SUPERBUILD_DIR "/usr/mitk-build")
set(QT_PATH "/home/pop_os")

set(CMAKE_PREFIX_PATH
        "${MITK_SUPERBUILD_DIR}/MITK-build/"
        "${MITK_SUPERBUILD_DIR}/ep/src/VTK/CMake/"
        "${QT_PATH}/Qt/5.12.8/gcc_64/lib/cmake/Qt5/"
        )

find_package(MITK CONFIG REQUIRED)
find_package(OpenMP REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)


set(cpp_files
        DataManagerView.cpp
        QmitkDataManagerItemDelegate.cpp
        )

set(header_files
        DataManagerView.h
        QmitkDataManagerItemDelegate.h
        )

set(ui_files
        DataManagerView.ui
        )

add_library(${PROJECT_NAME} STATIC ${cpp_files} ${header_files} ${ui_files})

target_compile_definitions(${PROJECT_NAME} PUBLIC cxx_std_14)

set(AppDataManager_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../AppDataManager/")

###
# Checks whether this project is the main one or a subdirectory.
###
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
        # I am top-level project.
        add_subdirectory(${AppDataManager_PATH} build/)
else()
        # I am called from other project with add_subdirectory().

        # Required on Unix OS family to be able to be linked into shared libraries.
        set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
###

target_link_libraries(${PROJECT_NAME} PUBLIC
        MitkCore
        MitkQtWidgetsExt
        Qt5::Widgets
        AppDataManager
        )

target_include_directories(${PROJECT_NAME} PUBLIC ${AppDataManager_PATH})

These pieces of code are part of an open-source project currently in development at the time I'm writing this answer.

Link: https://github.com/Oshio09/covid-gui

Oshio
  • 133
  • 4
  • 17
  • If you intend to compile and run Step8 example from MITK, it will return segmentation fault. It has been fixed in June 19, 2020, see [here](https://phabricator.mitk.org/T27389). In the link provided in my answer you have the fixed version inside examples folder. Be aware that Step8 depends on Step6 and Step7. – Oshio Jun 21 '20 at 16:20