CMAKE_PREFIX_PATH并不能帮助CMake找到Qt5



从这里: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-not-find-a-package-configuration-file-file-provider-by-qt5widgets/
https://askubuntu.com/questions/508503/whats-the-development-package-for-qt5-in-14-04

用于MacOS在终端中导出以下变量。

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

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

发行构建重新配置

cmake ../

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

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

另一个解决方案,如果您在主目录中手动安装了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将在子目录中找到合适的配置文件。

cmake错误消息有关find_package和设置 cmake_prefix_path_path varible

将" QT5"的安装前缀添加到cmake_prefix_path

以某种方式具有误导性。它仅谈论"安装前缀",但是此安装仍然需要包含Qt5Config.cmakeqt5-config.cmake文件,以可通过find_package

但是消息

如果" qt5"提供了 单独的开发软件包或SDK,请确保已安装它。

很清楚:

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


上面的所有内容仅适用于 find_package config 模式,当"查找"脚本既不由cmake提供,也不适用于使用此命令的cmake项目。

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

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

这将打印出Cmake确实找到包装文件的查找过程。

对于macOS,我使用cmake应用程序来阐明qt5_dir,以"源代码为source"。在我的CPP文件目录和"在哪里构建二进制文件"的目录。到构建文件夹。然后,放入"/usr/local/cellar/qt@5/5.15.15.8_3/lib/cmake/qt"对于实际的qt5_dir值。随着新更新的发布,该目录可能会随着时间的推移而变化,因此您必须每次手动查找qt5config.cmake文件才能更新qt5_dir

相关内容

  • 没有找到相关文章

最新更新