Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Windows上查找Qt5 CMake模块_C++_Windows_Visual C++_Cmake_Qt5 - Fatal编程技术网

C++ 如何在Windows上查找Qt5 CMake模块

C++ 如何在Windows上查找Qt5 CMake模块,c++,windows,visual-c++,cmake,qt5,C++,Windows,Visual C++,Cmake,Qt5,我正在尝试在Windows上使用CMake制作一个非常基本的Qt5应用程序。 我使用了文档,我的main.cpp文件只包含一个main函数 我的CMakeLists.txt就是: cmake_minimum_required(VERSION 2.8.9) project(testproject) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMa

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

我的CMakeLists.txt就是:

cmake_minimum_required(VERSION 2.8.9)

project(testproject)

# 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)
在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!
你有什么想法吗?

在台词后面

cmake_minimum_required(VERSION 2.8.9)

project(testproject)


这就解决了问题。

您应该设置CMAKE_PREFIX_PATH环境变量,或者使用CMAKE gui设置Qt 5包的路径。

一种方法是在Qt Creator中打开CMakeLists.txt。Qt Creator本机支持CMake,它总是知道Qt在哪里

只需将Qt路径添加到Windows%path%变量中即可。正如官方文档中所建议的:

这里有一种技术,它利用cmake读取注册表的能力,强制注册表值定位匹配的msvc的Qt5Config.cmake

它试图通过对其中的各种5.x文件夹名称(例如C:\Qt\)进行反向排序来使用可用的最高Qt5版本

也可以将其放置在模块内,例如QtLocator.cmake

然后

# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
这本书完美地涵盖了整个想法。只有一件事需要补充:微软的版本控制似乎正在转变为几何级数。该系列太短,尚未确认,因此从2019年起,可使用以下公式:

# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
# - see also https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
if ((MSVC_VERSION GREATER_EQUAL "1920") AND (IS_DIRECTORY "${QT_VERSION}/msvc2019"))
    set(QT_MSVC "2019")
elseif ((MSVC_VERSION GREATER_EQUAL "1910") AND (IS_DIRECTORY "${QT_VERSION}/msvc2017"))
    set(QT_MSVC "2017")
elseif (MSVC_VERSION GREATER_EQUAL "1900")
    set(QT_MSVC "2015")
else ()
    MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 500) / 100")
endif ()

我也有同样的问题。我在使用cmake 2.8.9,并试图用cmake_模块_路径做同样的事情。它不理我。使用CMAKE_前缀_路径,它工作了。可能,这个版本的cmake发生了一些变化。。。谢谢不要要求您的用户那样更改CMakeLists。改为设置环境变量,正如Qt文档所说的那样。@steveire我同意,但您能指出Qt文档中解释的地方吗?添加到哪里?哪个文件?哪一行?我相信@steveire的意思是设置CMAKE_PREFIX_PATH=。。。windows或导出CMAKE_前缀_路径=。。。在运行cmake/cmake gui之前与UNIX类似。在Windows上,您可以创建一个设置此路径的批处理文件,然后启动cmake-gui。这完全不是真的。请解释一下原因?即使Qt不在路径中,我也可以通过Qt Creator进行配置。相反,CMake GUI对Qt没有任何线索。我还建议人们查看dzada的答案,以获得找到CMake所需Qt前缀的路径示例。人们应该听这个家伙的。
# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
# - see also https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
if ((MSVC_VERSION GREATER_EQUAL "1920") AND (IS_DIRECTORY "${QT_VERSION}/msvc2019"))
    set(QT_MSVC "2019")
elseif ((MSVC_VERSION GREATER_EQUAL "1910") AND (IS_DIRECTORY "${QT_VERSION}/msvc2017"))
    set(QT_MSVC "2017")
elseif (MSVC_VERSION GREATER_EQUAL "1900")
    set(QT_MSVC "2015")
else ()
    MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 500) / 100")
endif ()