CMAKE_PREFIX_PATH无法帮助CMake找到Qt5。

29

从这里开始:https://dev59.com/CGEh5IYBdhLWcg3wnEnb#28327499

我尝试了这个:

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

我在运行cmake时使用了-DCMAKE选项,但输出仍然相同:

这里我使用了-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
7个回答

51

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

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} 吗? - isnullxbh
另外,target_link_libraries 与 qt5_use_modules 重复,因此 CMake 会生成错误。 - isnullxbh
1
在我的情况下,我还必须安装 qttools5-dev,因为在安装了上述软件包之后,我仍然会收到“找不到Qt5Linguist”错误。 - Aditya Sriram
1
第一个包对我来说已经足够了。无论如何,要自己找到这个解决方案是很困难的,非常感谢! - Ploumploum
1
你刚刚救了我 - undefined

9
在 macOS 中,在终端中导出以下变量。
export CMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.15.1/  

可能会有另一个版本,请检查您拥有的版本并相应更改最后路径组件。

发出构建重新配置命令。

cmake ../

这也解决了在Ubuntu 20.04上的问题,我已经将Qt5 dev文件安装到默认位置。export CMAKE_PREFIX_PATH=/usr/local/Qt-5.15.2/. - Roger Dahl
对于我来说:export QT_DIR=/usr/local/Cellar/qt/5.14.2/export Qt5_DIR=/usr/local/Cellar/qt/5.14.2/ - someone

7

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

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

4

如果你已经手动安装了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将在子目录中找到合适的配置文件。


2

几年后,我在使用新的Android NDK工具链时遇到了类似的问题。这里提供的解决方案不够。我找到了一种新的方法,并在这里发布供大家参考:

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

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

2
CMake错误信息涉及失败的find_package和设置CMAKE_PREFIX_PATH变量。

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

这有些误导人。它只谈论“安装前缀”,但是此安装仍需要包含Qt5Config.cmake或qt5-config.cmake文件,以便通过find_package进行发现。
但是消息

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

是清晰的:
需要安装一个包含所需配置文件的开发包。
以上所有内容仅适用于find_package的CONFIG模式,当“Find”脚本既不由CMake提供也不由使用此命令的CMake项目提供时。

0
对于MacOS,我使用CMake应用程序通过将“源代码位置”设置为我的cpp文件目录和“二进制生成位置”设置为构建文件夹来澄清Qt5_DIR。然后,将实际的Qt5_DIR值放入“/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt”。随着新的更新发布,该目录可能会发生更改,因此您每次更新Qt5_DIR时都必须手动查找Qt5Config.cmake文件。

当使用CMake时,我的生成器被设置为Unix Makefile而不是XCode。请注意将 "/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt" 更改为 "/usr/local/Cellar/qt@5/5.15.8_3/lib/cmake/Qt5"。 - Evan Abbott
第一部分不太清楚,“澄清Qt5_DIR的含义是什么?”它与您的cpp文件和二进制文件有什么关系?这难道不是关于qt5配置文件的位置吗?第二部分:这不是与此答案相同吗? - Mihoub Abderrahmene Rayene

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接