我正在尝试使用CMake(而不是使用QT Creator(设置QT项目。我目前正在尝试构建一个最低限度的工作示例,它只显示一个空窗口。我目前的代码是基于(更像coppied(官方(?(文档中的本教程和另一位用户对类似问题的回答。
然后我运行以下三个命令:
cmake .
cmake --build .
.Debughelloworld.exe
但是什么也没发生(当运行程序时,前两个命令似乎按预期工作(。如果我将main.cpp中的代码放入现有的QTCreator项目中,它就会工作并显示一个空窗口。有人能告诉我,我缺了什么?
main.cpp
#include <QApplication>
#include <QWidget>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//Option 1, like in the mentioned stackoverflow answer
//QWidget window;
//Option 2, to test If maybe the QWidget above was the problem
QMainWindow window;
window.setWindowTitle("Test");
window.show();
return app.exec();
}
CMakeList.txt
cmake_minimum_required(VERSION 3.16)
project(helloworld VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Without this line, cmake does not find the package
set(CMAKE_PREFIX_PATH "C:/Qt/6.3.1/msvc2019_64")
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
add_executable(helloworld
main.cpp
)
target_link_libraries(helloworld PRIVATE Qt6::Widgets)
set_target_properties(helloworld PROPERTIES
WIN32_EXECUTABLE ON
MACOSX_BUNDLE ON
)
编辑:根据Aamir的建议,我创建了一个额外的构建文件夹。三个指令的输出如下:
还有一件事,如果我用一个简单的";Hello World"程序,它运行良好。因此,我认为第一个cmake命令中丢失/失败的部分没有问题。还是我错了?
PS H:CodingQT_Tic_Tac_toe> mkdir build
Directory: H:CodingQT_Tic_Tac_toe
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 19/08/2022 10:11 build
PS H:CodingQT_Tic_Tac_toe> cd .build
PS H:CodingQT_Tic_Tac_toebuild> cmake ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
-- The CXX compiler identification is MSVC 19.32.31332.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE
-- Performing Test HAVE_STDATOMIC
-- Performing Test HAVE_STDATOMIC - Success
-- Found WrapAtomic: TRUE
-- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: H:/Coding/QT_Tic_Tac_toe/build
PS H:CodingQT_Tic_Tac_toebuild> cmake --build .
Microsoft (R) Build Engine version 17.2.1+52cd2da31 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Automatic MOC and UIC for target helloworld
Building Custom Rule H:/Coding/QT_Tic_Tac_toe/CMakeLists.txt
mocs_compilation_Debug.cpp
main.cpp
Generating Code...
helloworld.vcxproj -> H:CodingQT_Tic_Tac_toebuildDebughelloworld.exe
Building Custom Rule H:/Coding/QT_Tic_Tac_toe/CMakeLists.txt
PS H:CodingQT_Tic_Tac_toebuild> .Debughelloworld.exe
PS H:CodingQT_Tic_Tac_toebuild>
您需要将Qt DLL目录永久添加到PATH
环境变量中,或者在从控制台调用程序之前添加,如下所示:
set PATH=C:Qt6.3.1msvc2019_64bin;%PATH%
如果您从cmd
控制台窗口启动应用程序,则会弹出一个错误对话框,通知您缺少DLL。但是,当您使用PowerShell
时,有一个挂起的错误(请参阅此处(阻止显示错误对话框。
注意:您可以从PowerShell
窗口调用cmd
来获取旧的控制台窗口。