【问题标题】:How can I build c++ Qt5 application by CMake in MSYS2如何在 MSYS2 中通过 CMake 构建 c++ Qt5 应用程序
【发布时间】:2019-07-22 08:35:06
【问题描述】:

我有一个 c++ qt5 "hello world" cmake 项目; 这是我的 main.cpp 和 CMakeLists.txt 文件:

#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QLabel>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QWidget widget;
    widget.resize(640, 480);
    widget.setWindowTitle("Hello, world!!!");

    QGridLayout *gridLayout = new QGridLayout(&widget);

    QLabel * label = new QLabel("Hello, world!!!");
    label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    gridLayout->addWidget(label);

    widget.show();

    return app.exec();
}
cmake_minimum_required(VERSION 3.1.0)

project(helloworld)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

message("cmake source dir = ${CMAKE_SOURCE_DIR}")

set (Qt5_DIR "/mingw64/lib/cmake/Qt5")

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(helloworld
    main.cpp
)

target_link_libraries(helloworld Qt5::Widgets)

我只是从 official page 复制 CMakeList 并写下一些非常基本的 main.cpp。 然后我在我的 Linux (openSUSE 15.0) 上构建它,一切正常。 现在我想在 MSYS2 上构建(获取 windows 版本)。首先我无法构建 cmake 项目,因为找不到 Qt5Config.cmake 文件。我加了

set (Qt5_DIR "/mingw64/lib/cmake/Qt5")

“手动”查找 Qt5Config.cmake 文件。 将此添加到我的 cmake 文件后,该项目已构建良好。 但是当我运行make 编译程序时,出现以下错误:

[ 25%] Automatic MOC and UIC for target helloworld
[ 25%] Built target helloworld_autogen
[ 50%] Linking CXX executable helloworld.exe
CMakeFiles/helloworld.dir/main.cpp.o:main.cpp:(.text$_ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1c): undefined reference to `QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)'
CMakeFiles/helloworld.dir/main.cpp.o:main.cpp:(.text$_ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1c): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/helloworld.dir/build.make:102: helloworld.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/helloworld.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

据我所知,有一些找不到的缺少的库。 但我不知道如何解决它。

【问题讨论】:

  • 你是如何安装 Qt 的?
  • @DavidGrayson, pacman -S mingw-w64-x86_64-qt5

标签: c++ cmake qt5 msys2


【解决方案1】:

你不需要 CMakeLists.tx 你可以按下构建按钮

【讨论】:

  • 你可以使用这个github.com/PLLUG/CPPQT-MSYS2-Cmder 在 qtcreator 和 Git 启动之后,你必须使用 2 个命令 install-dev-env 和 install-qt5-env
  • 抱歉,我没有任何构建按钮。我不会使用 cmake 在 linux 和 msys2 上以相同的方式构建我的应用程序。我只使用 vim 来创建我的 qt5 gui 应用程序。错了吗?
  • 你用过qtcreator
猜你喜欢
  • 2017-01-09
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2013-04-09
  • 2018-04-06
  • 1970-01-01
相关资源
最近更新 更多