2

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

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

我尝试将自己的代码分开,例如http://docs.mitk.org/nightly/BuildInstructionsPage.html。我使用这个 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)

但是 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 文件夹,并且 CMakeLists 应该包含该文件夹。但我不知道该怎么做。有什么建议吗?

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

4

1 回答 1

1

我要展示的内容仅在 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在静态库文件夹中调用 a 。这是用于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

于 2020-06-21T16:16:18.440 回答