CMakeList set CMAKE_PREFIX_PATH

29,102

Solution 1

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

Solution 2

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>")
Share:
29,102
Orion
Author by

Orion

Updated on March 18, 2021

Comments

  • Orion
    Orion about 3 years

    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)
    
  • Guillaume Racicot
    Guillaume Racicot about 3 years
    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.
  • CodeMonkey
    CodeMonkey about 3 years
    @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.
  • Guillaume Racicot
    Guillaume Racicot about 3 years
    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.