CMake with Qt to include modules xmlpatterns

2019-08-09 15:44发布

问题:

I am trying to switch from QMake to CMake. I get my first error like this:

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?

I wonder how to know or find the name of the module? Why it is find_package(Qt5Widgets) but not find_package(Qt5widgets)? Why it is target_link_libraries(project Qt5::Widgets) but not Qt5::widgets or Qt5Widgets? Where can I find these information? And the most important is how to use link the Qt XML Patterns?

My .pro file in QtCreator

#-------------------------------------------------
#
# 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

My CMakeList File

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)

Solution:

With a more detailed error message provided by find_package(Qt5XmlPatterns REQUIRED), I understanding CMake cannot find either Qt5XmlPatternsConfig.cmake or qt5xmlpatterns-config.cmake.

The required .cmake files should come with Qt. It turns out that my Qt directory is located in my home directory rather than \usr\lib. (I don't know how this happen. I should have install Qt with default options.)

So, I simply help cmake to locate the correct directory. In my case, all the .cmake files for Qt located under ~/Qt5.5.1/5.5/gcc_64/lib/cmake. Base on the document for find_package, it searches CMAKE_PREFIX_PATH CMAKE_FRAMEWORK_PATH CMAKE_APPBUNDLE_PATH for packages in turn. So I just add

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

for it to search correctly.

Finally, we can find the name of the package under Your_Qt_Install_directory/Qt5.5.1/5.5/gcc_64/lib/cmake".

回答1:

Use

find_package(Qt5XmlPatterns REQUIRED)

and then

target_link_libraries(name Qt5::XmlPatterns)

You had wrong case in the find_package too. The REQUIRED catches that.



标签: qt cmake