我将项目从Qt4迁移到Qt5。在Qt4中结合CMake,有变量QT_INCLUDE_DIR和QT_LIBRARY_DIR。他们在Qt5中失踪了。我需要的是替换这两个变量,因为我在Qt安装中有一些头/库(如QScintilla2)
我使用Qt5Widgets作为模块,我的第一个想法是使用此路径并使用..来获取父路径 - 但是Qt5Widgets_LIBRARIES返回库(名称)而不是路径(切断库?)。此外,Qt5Widgets_INCLUDE_DIRS的返回值看起来像一个巨大的(追加)路径(不同的包含目录之间没有空格)
那么 - 任何想法或解决方案?该解决方案应兼容Unix / Linux,OS X和Windows
答案 0 :(得分:2)
在玩了CMake之后,我来到了解决方案。我搜索包含,在找到它们之后,我使用了Qt5Widgets的库路径。在这种情况下,这可以毫无问题地工作,因为QScintilla将自己安装在Qt目录中(以及Linux下的lib目录):
# Module file for QScintilla - compiled with Qt5. These variables are available:
# QSCINTILLA2_FOUND = Status of QScintilla
# QSCINTILLA2_INCLUDE_DIR = QScintilla include dir
# QSCINTILLA2_LIBRARY = QScintilla library
# Check
if(${Qt5Widgets_FOUND})
# Set as not found
set(QSCINTILLA2_FOUND false)
# Iterate over the include list of the Qt5Widgets module
foreach(TEMPPATH in ${Qt5Widgets_INCLUDE_DIRS})
# Check for a Qsci directory
find_path(QSCINTILLA2_INCLUDE_DIR qsciglobal.h ${TEMPPATH}/Qsci)
# Found - break loop
if(QSCINTILLA2_INCLUDE_DIR)
break()
endif()
endforeach()
# Check
if(QSCINTILLA2_INCLUDE_DIR)
# Get Qt5Widgets library and cut off the library name
get_target_property(QT5_WIDGETSLIBRARY Qt5::Widgets LOCATION)
get_filename_component(QT5_WIDGETSLIBRARYPATH ${QT5_WIDGETSLIBRARY} PATH)
# Add library
set(LIBRARYPATH ${QT5_WIDGETSLIBRARYPATH} "/usr/lib/" "/usr/local/lib")
find_library(QSCINTILLA2_LIBRARY NAMES libqscintilla2.a qscintilla2.lib PATHS ${LIBRARYPATH})
# Check
if(QSCINTILLA2_LIBRARY)
# Enable library
set(QSCINTILLA2_FOUND true)
mark_as_advanced(QSCINTILLA2_INCLUDE_DIR QSCINTILLA2_LIBRARY)
else()
message(FATAL_ERROR "QScintilla2 library not found")
endif()
else()
message(FATAL_ERROR "Cannot find QScintilla2 header")
endif()
else()
message(FATAL_ERROR "Qt5Widgets module not found")
endif()