我要展示的内容仅在 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