我们在每台开发 PC 上都有大量 Qt 副本——每个项目在第三方目录中都有自己的 Qt 副本。这是因为每个项目都是用自己的 Qt 开发和测试的;通常情况下,我们需要为某个项目定制 Qt 构建。因此,由于上面提到的,我们有一个单独的 SVN 存储库,用于保存各种 Qt 构建和每个 Qt 项目中的外部,它指的是这些构建中的一个。
问题是,在使用 CMake 时,我们只能选择 Qt 的一个版本,而 Qt4 finder 自己决定使用什么 Qt。这不是正确的。我们需要重定向查找器以配置项目第三方目录中特定 Qt 副本的所有内容。
我试图指定这样的PATHS
选项FIND_PACKAGE
:FIND_PACKAGE( Qt4 REQUIRED PATHS "thirdparty/Qt" )
但这没有帮助。我试图设置QT_QMAKE_EXECUTABLE
变量,但没有奏效。我尝试在安装之前将所需的 QMake 路径(FindQt4.cmake 使用它来确定所有目录)添加到 PATH 变量中,但这也无济于事。
有没有办法使用 FIND_PACKAGE 提供的所有东西,但对于某个 Qt 副本?我真的不想实现所有 Qt 包装器,手动设置所有定义、包含、目录等。
这是我的 CMake 项目。也许这会有所帮助:
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
#############################################################################
MACRO( ADD_FILES_TO_FILTER rootFilterName rootFilterPath files )
FOREACH( curFile ${files} )
FILE( RELATIVE_PATH curFilter "${CMAKE_CURRENT_SOURCE_DIR}/${rootFilterPath}" "${CMAKE_CURRENT_SOURCE_DIR}/${curFile}" )
FILE( RELATIVE_PATH test "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${curFile}" )
GET_FILENAME_COMPONENT( curFilter ${curFilter} PATH )
SET( curFilter "${rootFilterName}/${curFilter}" )
STRING( REPLACE "/" "\\\\" curFilter ${curFilter} )
SOURCE_GROUP( ${curFilter} FILES ${curFile} )
ENDFOREACH( curFile )
ENDMACRO( ADD_FILES_TO_FILTER rootFilterName rootFilterPath files )
MACRO( TO_RELATIVE_PATHS filePaths )
SET( resPaths "" )
FOREACH( curPath ${${filePaths}} )
FILE( RELATIVE_PATH relPath ${CMAKE_CURRENT_SOURCE_DIR} ${curPath} )
SET( resPaths ${resPaths} ${relPath} )
ENDFOREACH( curPath )
SET( ${filePaths} ${resPaths} )
ENDMACRO( TO_RELATIVE_PATHS filePaths )
######################################################################
# Define project settings
PROJECT( RealTimeCapture )
FIND_PACKAGE( Qt4 REQUIRED PATHS "thirdparty/Qt" )
SET( BOOST_ROOT "thirdparty/boost" )
FIND_PACKAGE( Boost REQUIRED )
# Collect all required files for build
FILE( GLOB_RECURSE headers RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/RTCF/include/*.h" )
FILE( GLOB_RECURSE sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/RTCF/src/*.cpp" )
FILE( GLOB_RECURSE resources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.qrc" )
FILE( GLOB_RECURSE forms RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.ui" )
# Preprocess all Qt files
QT4_WRAP_CPP( header_mocs ${headers} )
QT4_WRAP_UI( form_headers ${forms} )
QT4_ADD_RESOURCES( resources_rcc ${resources} )
# Convert all generated files paths to relative ones
TO_RELATIVE_PATHS( header_mocs )
TO_RELATIVE_PATHS( form_headers )
TO_RELATIVE_PATHS( resources_rcc )
# Create executable
ADD_EXECUTABLE( RealTimeCapture ${headers} ${sources} ${header_mocs} ${form_headers} ${resources_rcc} )
SET_TARGET_PROPERTIES( RealTimeCapture PROPERTIES COMPILE_FLAGS "/Zc:wchar_t-" )
# Set filters for project according to its namespaces
FILE( RELATIVE_PATH buildDir ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
ADD_FILES_TO_FILTER( "Headers" "src/RTCF/include" "${headers}" )
ADD_FILES_TO_FILTER( "Sources" "src/RTCF/src" "${sources}" )
ADD_FILES_TO_FILTER( "Generated" "${buildDir}/src/RTCF/include" "${header_mocs}" )
ADD_FILES_TO_FILTER( "Forms" "${buildDir}/src/RTCF/include" "${form_headers}" )
ADD_FILES_TO_FILTER( "Resources" "${buildDir}" "${resources_rcc}" )
# Set additional include directories
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( "src" )
INCLUDE_DIRECTORIES( "src/RTCF/include" )
INCLUDE_DIRECTORIES( "thirdparty" )
# Configure Qt
SET( QT_USE_QTNETWORK TRUE )
SET( QT_USE_QTSQL TRUE )
SET( QT_USE_QTSCRIPT TRUE )
SET( QT_USE_QTXML TRUE )
SET( QT_USE_QTWEBKIT TRUE )
INCLUDE( ${QT_USE_FILE} )
ADD_DEFINITIONS( ${QT_DEFINITIONS} )
TARGET_LINK_LIBRARIES( RealTimeCapture ${QT_LIBRARIES} )
# Add boost support
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
TARGET_LINK_LIBRARIES( RealTimeCapture ${Boost_LIBRARIES} )
# Add other libs include dirs
INCLUDE_DIRECTORIES( "thirdparty/SimpleCrypt" )
INCLUDE_DIRECTORIES( "thirdparty/id3lib/include" )
INCLUDE_DIRECTORIES( "thirdparty/libtwitcurl" )
INCLUDE_DIRECTORIES( "thirdparty/curl-7.24.0/curl/include" )
INCLUDE_DIRECTORIES( "thirdparty/log4qt/include" )
INCLUDE_DIRECTORIES( "thirdparty/fervor-auto" )
# Add defines
ADD_DEFINITIONS( RealTimeCapture -DQUAZIP_STATIC )
ADD_DEFINITIONS( RealTimeCapture -DBUILDING_LIBCURL )
ADD_DEFINITIONS( RealTimeCapture -DID3LIB_LINKOPTION=1 )
ADD_DEFINITIONS( -DUNICODE -D_UNICODE ) #enable unicode support
更新
正如对已接受答案的评论中提到的那样,我设法通过设置QT_QMAKE_EXECUTABLE
CMake 变量来设置自定义 qmake.exe (不同于添加到 OS PATH 变量中的那个)。但问题是,qmake 可执行文件具有所有 Qt 模块的硬编码路径。所以我通过提供一个自定义qt.conf
文件来解决它(正如答案中提到的那样)。我在互联网上没有找到任何示例,所以在这里,也许它会帮助某人:
[Paths]
Prefix=..
Documentation=doc
Headers=include
Libraries=lib
Binaries=bin
Plugins=plugins
Imports=imports
Data=
Translations=
Settings=
Examples=
Demos=
我正是用这个。您可以考虑在前缀中指定 Qt 安装的绝对路径。我只是不想在 repo 中使用绝对路径,它可以克隆到任意位置。有关使用的详细信息,请参阅此链接qt.conf
。