2

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".

6
  • 1
    Can you try XmlPatterns instead of xmlpatterns (note the casing)? In this case I suspect it's most like Qt following the CMake casing conventions. Commented Jan 15, 2016 at 7:46
  • I did try all sorts of casing combination Xmlpatterns, XmlPatterns.... Commented Jan 15, 2016 at 7:51
  • 1
    What's the output of the CMake run? I asuume it does not find Qt5Xmlpattern. I thought you have to look for ` find_packages(Qt5 COMPONENTS Core Gui Widgets XmlPatterns)` or similar. Commented Jan 15, 2016 at 8:00
  • For things not mentioned in doc.qt.io/qt-5/cmake-manual.html , I think your best bet is to read the source of the FindQt* scripts. Commented Jan 15, 2016 at 14:50
  • @FrankOsterfeld Right. There is not many examples regarding Qt5 with CMake on the net. Commented Jan 16, 2016 at 3:19

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

REQUIRED does nothing but print an error message in more detail if the package cannot be found. It is not compulsory to be here. But thanks for your comment, I get the reason why I cannot find the package. I will put it in my question ASAP.
Yes. REQUIRED points you to your first error because of the case incorrectness. Even for optional dependencies, I use REQUIRED, and only remove it before pushing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.