简体   繁体   English

如何在MSYS2中通过CMake构建C ++ Qt5应用程序

[英]How can I build c++ Qt5 application by CMake in MSYS2

I have a c++ qt5 "hello world" cmake project; 我有一个c ++ qt5“ hello world” cmake项目; Here are my main.cpp and CMakeLists.txt files: 这是我的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)

I just copy CMakeList from official page and write down some very basic main.cpp. 我只是从官方页面复制CMakeList并写下一些非常基本的main.cpp。 Then I built it on my Linux (openSUSE 15.0) and everything works fine. 然后,我在Linux(openSUSE 15.0)上构建了它,一切正常。 Now I want to build in on MSYS2 (to get windows version). 现在我想在MSYS2上构建(获取Windows版本)。 First I could not build cmake project because Qt5Config.cmake file was not found. 首先,由于找不到Qt5Config.cmake文件,因此无法构建cmake项目。 I added 我加了

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

to find Qt5Config.cmake file "manually". 查找“手动” Qt5Config.cmake文件。 After adding this to my cmake file the project had been built fine. 将其添加到我的cmake文件后,该项目已构建良好。 But when I run make to compile program I get the following errors: 但是,当我运行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

As I can understand there are some missing libraries that could not be found. 据我了解,有一些找不到的缺少库。 But I do not know how to fix it. 但是我不知道如何解决它。

您不需要CMakeLists.tx,您只需按构建按钮

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

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