如何将 MITK 用作库

  • 本文关键字:MITK c++ mitk
  • 更新时间 :
  • 英文 :


我知道这是个老问题,但我试图搜索StackOverFlow,没有找到任何适合我的好答案。所以我试图重新发布这个。

我正在使用 linux 并尝试使用 CMake 和 Make 命令构建代码。

我试图像 http://docs.mitk.org/nightly/BuildInstructionsPage.html 一样分离我自己的代码。我使用这个CMakeList.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)

但是find_package抛出错误:

Could not find a package configuration file provided by "MITK" with any of the following names:
MITKConfig.cmake
mitk-config.cmake

正如我从cmake教程中读到的那样,它应该是一个包含"MITKConfig.cmake"文件的cmake文件夹,CMakeList应该包含该文件夹。但我找不到该怎么做。有什么建议吗?

P/S:使用启动库是我们自己的项目中包含外部库的一个很好的例子。有谁知道如何用 MITK 图书馆做到这一点?

我正在展示的内容仅在 Pop!_OS 18.04 上的 2018.04.99 版本中进行了测试。

Step3示例可以使用以下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
)

我正在开发一个更复杂的应用程序,它被分成模块(静态库(,我有一个顶级CMakeLists.txt调用子项目:

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()允许 IDE 查找静态库 .h 和.cpp文件。对于MITK来说,这已经由find_package().

add_subdirectory()调用静态库文件夹中的CMakeLists.txt。这是用于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
)

可能存在依赖于另一个模块的模块(静态库(。AppDataManager由顶级应用程序和DataManagerView使用。如果我们使用类似于上一个CMakeLists.txt,我们可以成功地将DataManagerView编译为独立的库,但由于我们调用add_subdirectory(modules/AppDataManager)两次,顶级将失败,为了避免这种情况,我想出了这个 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})

这些代码片段是我写这个答案时目前正在开发的开源项目的一部分。

链接: https://github.com/Oshio09/covid-gui

相关内容

  • 没有找到相关文章