使用Qt进行CMake以包含模块xmlpatterns

时间:2016-01-15 07:41:31

标签: qt cmake

我正在尝试从QMake切换到CMake。我得到了这样的第一个错误:

Target "aacmakeqt" links to target "Qt::xmlpatterns" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?

我想知道如何知道或找到模块的名称?为什么是find_package(Qt5Widgets)而不是find_package(Qt5widgets)? 为什么是target_link_libraries(project Qt5::Widgets)但不是Qt5::widgetsQt5Widgets?我在哪里可以找到这些信息?最重要的是如何使用链接Qt XML Patterns

QtCreator中的.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2015-11-07T19:23:43
#
#-------------------------------------------------   
QT       += core gui xmlpatterns widgets

TARGET = myapp
TEMPLATE = app

SOURCES += main.cpp ......... 
HEADERS  += mainwindow.h ........
FORMS    += mainwindow.ui ........
RESOURCES += resources.qrc

我的CMakeList文件

cmake_minimum_required(VERSION 3.0)

## Setting ProjectId by current directory
get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
MESSAGE(STATUS "\tProjectId: " ${ProjectId} ) ## Debug Message

## Defining project
project(${ProjectId})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Find the library
find_package(Qt5Widgets)
find_package(Qt5Xmlpatterns)

MESSAGE(STATUS "Project Source Directory: " ${PROJECT_SOURCE_DIR} ) ## Debug Message

file(GLOB_RECURSE SRC_CPP ${PROJECT_SOURCE_DIR} *.cpp)
MESSAGE(STATUS "\tsource: " ${SRC_CPP}) ## Debug Message
add_executable(${ProjectId} ${SRC_CPP})

# Use the Widgets module from Qt 5.
target_link_libraries(${ProjectId} Qt5::Widgets)
target_link_libraries(${ProjectId} Qt5::xmlpatterns)

解决方案:

如果find_package(Qt5XmlPatterns REQUIRED)提供了更详细的错误消息,我理解CMake无法找到Qt5XmlPatternsConfig.cmakeqt5xmlpatterns-config.cmake

Qt应附带所需的.cmake个文件。事实证明我的Qt目录位于我的主目录而不是\usr\lib。 (我不知道这是怎么发生的。我应该用默认选项安装Qt。)

所以,我只是帮助cmake找到正确的目录。就我而言,Qt的所有.cmake个文件都位于~/Qt5.5.1/5.5/gcc_64/lib/cmake下。 根据{{​​3}}的文档,它会依次搜索CMAKE_PREFIX_PATH CMAKE_FRAMEWORK_PATH CMAKE_APPBUNDLE_PATH的包。所以我只想添加

set(CMAKE_FRAMEWORK_PATH ${CMAKE_FRAMEWORK_PATH} "~/Qt5.5.1/5.5/gcc_64/lib/cmake")

正确搜索。

最后,我们可以在Your_Qt_Install_directory/Qt5.5.1/5.5/gcc_64/lib/cmake"下找到包名称。

1 个答案:

答案 0 :(得分:2)

使用

find_package(Qt5XmlPatterns REQUIRED)

然后

target_link_libraries(name Qt5::XmlPatterns)

你在find_package也有错误的案例。 REQUIRED抓住了这个。