简体   繁体   中英

How to use MITK as a library

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM