28

From here: https://stackoverflow.com/a/28327499/462608

I tried this:

cmake_minimum_required(VERSION 2.8.12)

project(qtquick_hello_cmake)

set(CMAKE_PREFIX_PATH "/opt/Qt5.9.1/5.9.1/")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 COMPONENTS Quick Core REQUIRED)

qt5_add_resources(RESOURCES qml.qrc)

add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")

qt5_use_modules(${PROJECT_NAME} Quick Core)

target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick)

Here is the output of cmake .

:~/junk/qtquick_hello_cmake$ cmake .
CMake Error at CMakeLists.txt:11 (find_package):
  By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt5", but
  CMake did not find one.

  Could not find a package configuration file provided by "Qt5" with any of
  the following names:

    Qt5Config.cmake
    qt5-config.cmake

  Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
  to a directory containing one of the above files.  If "Qt5" provides a
  separate development package or SDK, be sure it has been installed.


-- Configuring incomplete, errors occurred!
See also "/home/.../junk/qtquick_hello_cmake/CMakeFiles/CMakeOutput.log".

This is to show that /opt/Qt5.9.1/ does exist.

:~/junk/qtquick_hello_cmake$ cd /opt/Qt5.9.1/5.9.1/
:/opt/Qt5.9.1/5.9.1$ ls
android_armv7  android_x86  gcc_64  Src

Here I run the cmake with -DCMAKE option, but the output is still same:

:~/junk/qtquick_hello_cmake$ cmake -DCMAKE_PREFIX_PATH=/opt/Qt5.9.1/5.9.1/ -DWITH_QT5=1 .
CMake Error at CMakeLists.txt:11 (find_package):
  By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt5", but
  CMake did not find one.

  Could not find a package configuration file provided by "Qt5" with any of
  the following names:

    Qt5Config.cmake
    qt5-config.cmake

  Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
  to a directory containing one of the above files.  If "Qt5" provides a
  separate development package or SDK, be sure it has been installed.


-- Configuring incomplete, errors occurred!
See also "/home/.../junk/qtquick_hello_cmake/CMakeFiles/CMakeOutput.log".

Contents of the directory:

:~/junk/qtquick_hello_cmake$ ls
CMakeCache.txt  CMakeFiles  CMakeLists.txt  main.cpp  main.qml  qml.qrc
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

7 Answers7

49

I installed the following missing packages:

sudo apt-get install qtbase5-dev
sudo apt-get install qtdeclarative5-dev

Attaching any kind of prefix is not required now:

CMakeList:

    :~/junk/qtquick_hello_cmake$ cat CMakeLists.txt
    cmake_minimum_required(VERSION 2.8.12)
    
    project(qtquick_hello_cmake)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    find_package(Qt5 COMPONENTS Quick Core REQUIRED)
    
    qt5_add_resources(RESOURCES qml.qrc)
    
    add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
    
    qt5_use_modules(${PROJECT_NAME} Quick Core)
    
    target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick)

New output:

:~/junk/qtquick_hello_cmake$ ls
build  CMakeLists.txt  main.cpp  main.qml  qml.qrc

:~/junk/qtquick_hello_cmake$ cd build/
:~/junk/qtquick_hello_cmake/build$ rm -rf *

:~/junk/qtquick_hello_cmake/build$ cmake ../
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../junk/qtquick_hello_cmake/build

Errors are gone now.

Thanks to:
https://answers.ros.org/question/236324/could-not-find-a-package-configuration-file-provided-by-qt5widgets/
https://askubuntu.com/questions/508503/whats-the-development-package-for-qt5-in-14-04

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
9

For macOS export the following variable in the terminal.

export CMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.15.1/  

There may be another version, check which one you have and change the last path component accordingly.

Issue the build reconfiguration

cmake ../
compor
  • 2,239
  • 1
  • 19
  • 29
3x.
  • 91
  • 1
  • 2
  • This resolved it on Ubuntu 20.04 as well, where I had installed Qt5 dev files to the default location. `export CMAKE_PREFIX_PATH=/usr/local/Qt-5.15.2/`. – Roger Dahl Sep 04 '21 at 00:21
  • for me: `export QT_DIR=/usr/local/Cellar/qt/5.14.2/`, `export Qt5_DIR=/usr/local/Cellar/qt/5.14.2/` – someone Oct 21 '21 at 03:57
7

I encountered this problem recently, and I solved this by adding this to any CMakeLists.txt that produced this error:

set(Qt5_DIR "*PATH TO YOUR QT QT5CONFIG FILE HERE*" CACHE PATH "Initial cache" FORCE)
karel
  • 5,489
  • 46
  • 45
  • 50
rguessford
  • 370
  • 4
  • 10
4

Another solution, if you have manually installed Qt in your home directory for example (instead of using the system one), is to specify exactly the version you want to use in the command line. Example:

cmake \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SAMPLES=TRUE \
  -DBUILD_STATIC_QRC=TRUE \
  -DCMAKE_PREFIX_PATH=/home/anthony/Qt/5.15.2/gcc_64 \
  ..

Take care to specify the version + the target system you are using. Then, Qt will find the suitable configuration file in the subdirectory.

li ki
  • 342
  • 3
  • 11
A. Rabine
  • 51
  • 3
2

CMake error message about failed find_package and setting CMAKE_PREFIX_PATH variable

Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH

is somehow misleading. It talks only about "installation prefix", but this installation still require to contain Qt5Config.cmake or qt5-config.cmake files inside for being discoverable by find_package.

But the message

If "Qt5" provides a separate development package or SDK, be sure it has been installed.

is clear:

One need to install a development package which contains required config files.


Everything above is applicable only to CONFIG mode of find_package, when "Find" script is provided neither by CMake nor by CMake project which uses this command.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
2

Several years later, I met a similar problem with the new Android NDK toolchain. The solutions provided here were not enough. I found a new method and post here for the following:

$ cmake --log-level=DEBUG --debug-find <SRC_DIR>

This will print out the find procedure that CMake did find the package file.

Holmes Conan
  • 1,208
  • 12
  • 21
0

For MacOS, I used the CMake app to clarify Qt5_DIR by setting "Where is source code" to my cpp file's directory and "Where to build the binaries" to the build folder. Then, put in "/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt" for the actual Qt5_DIR value. The directory may change over time as new updates are released, so you will have to manually find the Qt5Config.cmake file each time to update Qt5_DIR

  • Edit: replace "/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt" with "/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt5" When using CMake, I had the generator set to Unix Makefile instead of XCode – Evan Abbott Jun 02 '23 at 16:59
  • The first part is unclear, what does clarifying `Qt5_DIR` mean? and how is it related to your cpp files and binaries? Isn't it about the location of qt5 config files? Second part: isn't it the same as this [answer](https://stackoverflow.com/a/51737321/17726418)? – Abderrahmene Rayene Mihoub Jun 02 '23 at 17:47