【问题标题】:CMAKE_PREFIX_PATH doesn't help CMake in finding Qt5CMAKE_PREFIX_PATH 不能帮助 CMake 找到 Qt5
【发布时间】:2018-05-18 17:24:46
【问题描述】:

从这里:https://stackoverflow.com/a/28327499/462608

我试过了:

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)

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

这是为了表明/opt/Qt5.9.1/确实存在。

:~/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

这里我使用 -DCMAKE 选项运行 cmake,但输出仍然相同:

:~/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".

目录内容:

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

【问题讨论】:

    标签: linux qt cmake qt5 qtquick2


    【解决方案1】:

    我安装了以下缺失的软件包:

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

    现在不需要附加任何类型的前缀:

    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)
    

    新输出:

    :~/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
    

    错误现在消失了。

    感谢:
    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

    【讨论】:

    • "qml.qrc" - 可以替换为 ${RESOURCES} 吗?
    • 另外,target_link_libraries 是重复的 qt5_use_modules,所以,CMake 会产生错误。
    • 在我的情况下,我还必须安装 qttools5-dev,因为在安装上述软件包后,我仍然收到“找不到 Qt5Linguist”错误。
    • 第一个包对我来说已经足够了。反正我自己也很难找到这个解决方案,非常感谢!
    【解决方案2】:

    对于 macOS,在终端中导出以下变量。

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

    可能还有另一个版本,检查你有哪个版本并相应地更改最后一个路径组件。

    发布构建重新配置

    cmake ../
    

    【讨论】:

    • 这在 Ubuntu 20.04 上也解决了,我已将 Qt5 开发文件安装到默认位置。 export CMAKE_PREFIX_PATH=/usr/local/Qt-5.15.2/.
    • 对我来说:export QT_DIR=/usr/local/Cellar/qt/5.14.2/, export Qt5_DIR=/usr/local/Cellar/qt/5.14.2/
    【解决方案3】:

    我最近遇到了这个问题,我通过将它添加到任何产生此错误的 CMakeLists.txt 中来解决这个问题:

    set(Qt5_DIR "*PATH TO YOUR QT QT5CONFIG FILE HERE*" CACHE PATH "Initial cache" FORCE)
    

    【讨论】:

      【解决方案4】:

      关于失败 find_package 和设置 CMAKE_PREFIX_PATH 变量的 CMake 错误消息

      将“Qt5”的安装前缀添加到CMAKE_PREFIX_PATH

      在某种程度上具有误导性。它只谈论“安装前缀”,但此安装仍然要求在内部包含 Qt5Config.cmakeqt5-config.cmake 文件,以便 find_package 发现。

      但是消息

      如果“Qt5”提供了一个 单独的开发包或SDK,请确保已安装。

      很清楚:

      需要安装一个包含所需配置文件的开发包。


      以上所有内容仅适用于find_packageCONFIG模式,当“查找”脚本既不是由CMake提供,也不是由使用此命令的CMake项目提供时。

      【讨论】:

        【解决方案5】:

        另一种解决方案,例如,如果您在主目录中手动安装了 Qt(而不是使用系统安装的),则在命令行中准确指定要使用的版本。示例:

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

        注意指定版本+您正在使用的目标系统。然后,Qt 会在子目录中找到合适的配置文件。

        【讨论】:

          【解决方案6】:

          几年后,我在新的 Android NDK 工具链中遇到了类似的问题。这里提供的解决方案还不够。我找到了一种新方法并在此处发布以下内容:

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

          这将打印出 CMake 找到包文件的查找过程。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-13
            • 2020-04-10
            • 2016-11-28
            • 2023-02-23
            • 2019-09-18
            • 1970-01-01
            相关资源
            最近更新 更多