繁体   English   中英

使用 CMake 创建 QT 项目

[英]Creating a QT Project with CMake

我正在尝试使用 CMake 设置 QT 项目(而不是使用 QT Creator)。 我目前正在尝试构建一个最小的工作示例,它只显示一个空的 window。 我当前的代码基于(更像是复制)来自官方(?)文档的教程以及另一个用户对类似问题回答。

然后我运行以下三个命令:

cmake .
cmake --build .
.\Debug\helloworld.exe

但是什么也没有发生(运行程序时,前两个命令似乎按预期工作)。 如果我将 main.cpp 中的代码放入现有的 QT Creator 项目中,它可以工作并显示一个空的 window。 谁能告诉我,我错过了什么?

主文件

#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 的建议,我创建了一个额外的构建文件夹。 三个指令的output如下:

还有一件事,如果我用一个简单的“Hello World”程序替换 main.cpp 中的代码,它就可以正常工作。 因此,我假设第一个 cmake 命令中丢失/失败的部分没有问题。 还是我对那部分错了?

PS H:\Coding\QT_Tic_Tac_toe> mkdir build


    Directory: H:\Coding\QT_Tic_Tac_toe


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        19/08/2022     10:11                build


PS H:\Coding\QT_Tic_Tac_toe> cd .\build\
PS H:\Coding\QT_Tic_Tac_toe\build> 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:\Coding\QT_Tic_Tac_toe\build> 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:\Coding\QT_Tic_Tac_toe\build\Debug\helloworld.exe
  Building Custom Rule H:/Coding/QT_Tic_Tac_toe/CMakeLists.txt
PS H:\Coding\QT_Tic_Tac_toe\build> .\Debug\helloworld.exe
PS H:\Coding\QT_Tic_Tac_toe\build>

您需要将 Qt DLL 目录永久或在从控制台调用程序之前添加到PATH环境变量中,如下所示:

set PATH=C:\Qt\6.3.1\msvc2019_64\bin;%PATH%

如果您从cmd控制台 window 启动您的应用程序,则会弹出一个错误对话框,通知您缺少 DLL。 但是当您使用PowerShell时,存在一个待处理的错误(请参阅此处),该错误会阻止显示错误对话框。

注意:您可以从PowerShell window 调用cmd以获取旧控制台 Z05B8C74CBD96FBFZ4C1A352702。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM