22

If I run

cmake -DCMAKE_PREFIX_PATH=/home/rip/Qt/5.12.1/gcc_64/lib/cmake

everything works well. But if I write

set(CMAKE_PREFIX_PATH "/home/rip/Qt/5.12.1/gcc_64/lib/cmake")

in the CMakeLists.txt and run only cmake, the error message below is shown:

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

   Qt5QuickConfig.cmake
   qt5quick-config.cmake

This is the complete code:

set(CMAKE_CXX_FLAGS " -O3 -fopenmp")
set(CMAKE_PREFIX_PATH "/home/rip/Qt/5.12.1/gcc_64/lib/cmake")

project(${PROJECT_NAME} LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCES main.cpp Test5.cpp )
set(HAEDERS Test5.h )
set(RESOURCES qml.qrc )

find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} ${SOURCES} ${HAEDERS} ${RESOURCES}) 
target_compile_definitions(${PROJECT_NAME} PRIVATE $,$>:QT_QML_DEBUG>) 
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
Orion
  • 231
  • 1
  • 2
  • 8
  • 3
    Setting `CMAKE_PREFIX_PATH` from the `CMakeLists.txt` should work. Please, show **more code**: how your setting is placed relative to `project()` call and to `find_package()` call. – Tsyvarev Mar 12 '19 at 12:31
  • Hm, this setting should work. You may try to move `set(CMAKE_PREFIX_PATH ...)` after the `project()` call. Side notes: current setting of variable `CMAKE_CXX_FLAGS` definitely doesn't work, you need to move it after the `project()` call. Also, you code lacks for `cmake_minimum_required()` call. Usually, CMake emits a warning about this, so it is better to fix that. – Tsyvarev Mar 12 '19 at 21:56
  • Oh i found the error. It was a problem with Clion. Now all works fine. Thank you very much. But the 'CMAKE_CXX_FLAGS' works fine before the project(). – Orion Mar 13 '19 at 12:13
  • I have same issue with xtensor library setting CMAKE_PREFIX_PATH inside CMakeList.txt does not help but outside works well – Marat Zakirov Aug 26 '19 at 16:11
  • @Orion How did you work around this Clion issue? – jeffreyveon Sep 03 '19 at 12:36

2 Answers2

44

The prefix path is a list of paths. Use this:

list(APPEND CMAKE_PREFIX_PATH "/home/rip/Qt/5.12.1/gcc_64")

Also you don't have to point directly into the config file path, you also can point into the directory containing the lib/cmake/...

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • This worked for me. I am new to cmake and I wasted half an hour believing this does not work, only to find that the reason it didnt work was because I had a typo in my path (!), and cmake gave no warning about invalid path. – Thamme Gowda Jul 07 '23 at 23:40
  • 1
    @ThammeGowda The point of telling CMake to find package or find library is that the paths may or may not exist, I don't think it should warn about a non existent path ;) – Guillaume Racicot Jul 08 '23 at 12:43
3

For me @guillaume-racicot's suggstion doesn't work. The CMAKE_PREFIX_PATH is set, but everything that's on it is ignored.

I found out that you can access the environment variable, split that into a list and append to that to actually append to the prefix path instead of replacing it.

# Create a list by replacing colon with semicolon
string(REPLACE ":" ";" CMAKE_PREFIX_PATH "$ENV{CMAKE_PREFIX_PATH}")
# Append to the newly created list
list(APPEND CMAKE_PREFIX_PATH "<YOUR_ADDITIONAL_SEARCH_PATH>")
CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
  • That's weird. On what environment are you? It almost look like a bug to not treat it as a list in the first place. – Guillaume Racicot Mar 21 '21 at 13:22
  • @GuillaumeRacicot I'm on Ubuntu 18.04 with cmake 3.10.2. As far as I can tell, it's not that it's not treated as a list, but instead that `CMAKE_PREFIX_PATH` is empty, while `$ENV{CMAKE_PREFIX_PATH}` (the environment variable) is not, and in order to get anything that's on the environment variable, I need to first convert that string to a list. – CodeMonkey Mar 22 '21 at 10:35
  • 1
    Make sure you reproduce that in an otherwise empty CMake file. A file containing only `cmake_minimum_required(3.14 REQUIRED) project(test123) message("${CMAKE_PREFIX_PATH}")`. If you played with the prefix path or use toolchain file, it could be empty. If you use a toolchain file, I would advice *not* setting it back. – Guillaume Racicot Mar 22 '21 at 13:19