如何在Windows上找到Qt5 CMake模块

dza*_*ada 37 c++ windows cmake visual-c++ qt5

我正在尝试使用Windows上的CMake制作一个非常基本的Qt5应用程序.我使用Qt5的文档来使用CMake,我的main.cpp文件只包含一个main函数.

CMakeLists.txt的确如此:

cmake_minimum_required(VERSION 2.8.9)

project(testproject)
Run Code Online (Sandbox Code Playgroud)

编辑解决方案

set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")
Run Code Online (Sandbox Code Playgroud)

结束编辑

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld hello.cpp)

# Use the Widgets module from Qt 5.
qt5_use_modules(helloworld Widgets)
Run Code Online (Sandbox Code Playgroud)

在MSysGit bash中我输入`$ cmake -G"Visual Studio 11"

我得到这个输出:

$ cmake -G"Visual Studio 11"
-- The C compiler identification is MSVC 17.0.60204.1
-- The CXX compiler identification is MSVC 17.0.60204.1
-- Check for working C compiler using: Visual Studio 11
-- Check for working C compiler using: Visual Studio 11 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 11
-- Check for working CXX compiler using: Visual Studio 11 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Warning at CMakeLists.txt:11 (find_package):
  By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "Qt5Widgets", but CMake did not find one.

  Could not find a package configuration file provided by "Qt5Widgets" with
  any of the following names:

    Qt5WidgetsConfig.cmake
    qt5widgets-config.cmake

  Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set
  "Qt5Widgets_DIR" to a directory containing one of the above files.  If
  "Qt5Widgets" provides a separate development package or SDK, be sure it has
  been installed.


CMake Error at CMakeLists.txt:17 (qt5_use_modules):
  Unknown CMake command "qt5_use_modules".


-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

你有什么想法?

dza*_*ada 36

添加它可以解决问题.

set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")

我编辑了第一篇文章.

  • 不要求您的用户更改类似的CMakeLists.相反,设置环境变量,正如Qt文档所说的那样. (5认同)
  • @steveire 我同意,但你能指出解释这一点的 QT 文档吗? (2认同)
  • 添加在哪里?哪个文件?哪条线? (2认同)

ste*_*ire 18

您应该改为设置CMAKE_PREFIX_PATH环境变量,或使用cmake-gui设置Qt 5包的路径.


小智 5

您只需要将Qt路径添加到Windows%PATH%变量即可。如官方文档中所建议:http : //doc.qt.io/qt-4.8/install-win.html#step-3-set-the-environment-variables