linux - CMAKE_PREFIX_PATH 没有帮助 CMake 找到 Qt5

标签 linux qt cmake qt5 qtquick2

来自这里: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

最佳答案

我安装了以下缺少的包:

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

关于linux - CMAKE_PREFIX_PATH 没有帮助 CMake 找到 Qt5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647035/

相关文章:

c - 构建 MEX 文件时,在 ';' token 之前应为 ',' 、 ')' 或 '='

c++ - fatal error : X11/extensions/XIproto. h : No such file or directory #include <X11/extensions/XIproto. h>

c++ - Qt 的简单版本检查器

c++ - Qt 5.14 ubuntu 18.04 中的 cuda 10.2

c++ - 如何使用 cmake 创建共享库?

cmake - 使用 CMake 在 Qt Creator 4.0 Clang 静态分析器中启用 C++14

linux - 别名中的引用变量

php - 无法执行此命令

c++ - 如何在 Qt 程序中实现 IDv3 标签?

c++ - 如何在我的 cmake 项目中包含另一个 cmake 项目的头文件?