可以使用CMAKE构建QT-Widgets应用程序

发布于 2025-02-05 05:08:50 字数 3490 浏览 2 评论 0 原文

我正在尝试设置一个构建QT-Widgets应用程序的CMAKE项目,但无法正确编译。我的项目结构如下:

  • 包括/
    • mainwindow.hpp
  • 资源/
    • mainwindow.ui
  • src/
    • main.cpp
    • mainwindow.cpp
  • cmakelists.txt

cmakelists.txt

cmake_minimum_required(VERSION 3.10)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

project(Test VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS resources)

add_executable(App src/mainwindow.cpp src/main.cpp resources/mainwindow.ui)
target_include_directories(App PRIVATE include)
target_link_libraries(App PRIVATE Qt5::Widgets)

mainwindow.hpp

#ifndef MAINWINDOW_HPP_
#define MAINWINDOW_HPP_

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class Test : public QMainWindow {
    Q_OBJECT

public:
    Test(QWidget *parent = nullptr);
    ~Test();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_HPP_

mainwindow.cpp

#include "mainwindow.hpp"
#include "ui_mainwindow.h"

Test::Test(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);
}

Test::~Test(){
    delete ui;
}

main.cpp

#include "mainwindow.hpp"
#include <QApplication>


int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    Test w;
    w.show();
    return a.exec();
}

当我尝试构建项目时:

cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug
cmake --build build --target all

我会收到以下链接器错误:

[ 20%] Automatic MOC and UIC for target App
[ 20%] Built target App_autogen
[ 40%] Building CXX object CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o
[ 60%] Building CXX object CMakeFiles/App.dir/src/mainwindow.cpp.o
[ 80%] Building CXX object CMakeFiles/App.dir/src/main.cpp.o
[100%] Linking CXX executable App
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::Test(QWidget*)': .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::~Test()': .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/App.dir/build.make:132: App] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/App.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

查看CMAKES生成的,似乎正确链接了所需的对象文件,

/usr/bin/c++ \
    -g CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o \
       CMakeFiles/App.dir/src/mainwindow.cpp.o \
       CMakeFiles/App.dir/src/main.cpp.o \
    -o App \
       /usr/lib/libQt5Widgets.so.5.15.4 \
       /usr/lib/libQt5Gui.so.5.15.4 \
       /usr/lib/libQt5Core.so.5.15.4 

我不确定,为什么VTable引用不确定。我在这里做错了什么?

I am trying to setup a CMake Project building a Qt-Widgets application but can't compile it properly. My project structure is as follows:

  • include/
    • mainwindow.hpp
  • resources/
    • mainwindow.ui
  • src/
    • main.cpp
    • mainwindow.cpp
  • CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

project(Test VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS resources)

add_executable(App src/mainwindow.cpp src/main.cpp resources/mainwindow.ui)
target_include_directories(App PRIVATE include)
target_link_libraries(App PRIVATE Qt5::Widgets)

mainwindow.hpp

#ifndef MAINWINDOW_HPP_
#define MAINWINDOW_HPP_

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class Test : public QMainWindow {
    Q_OBJECT

public:
    Test(QWidget *parent = nullptr);
    ~Test();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_HPP_

mainwindow.cpp

#include "mainwindow.hpp"
#include "ui_mainwindow.h"

Test::Test(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);
}

Test::~Test(){
    delete ui;
}

main.cpp

#include "mainwindow.hpp"
#include <QApplication>


int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    Test w;
    w.show();
    return a.exec();
}

When I'm trying to build the project:

cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug
cmake --build build --target all

I get the following linker error:

[ 20%] Automatic MOC and UIC for target App
[ 20%] Built target App_autogen
[ 40%] Building CXX object CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o
[ 60%] Building CXX object CMakeFiles/App.dir/src/mainwindow.cpp.o
[ 80%] Building CXX object CMakeFiles/App.dir/src/main.cpp.o
[100%] Linking CXX executable App
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::Test(QWidget*)': .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::~Test()': .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/App.dir/build.make:132: App] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/App.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Looking at cmakes generated link.txt, it seems that the required object files are linked properly

/usr/bin/c++ \
    -g CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o \
       CMakeFiles/App.dir/src/mainwindow.cpp.o \
       CMakeFiles/App.dir/src/main.cpp.o \
    -o App \
       /usr/lib/libQt5Widgets.so.5.15.4 \
       /usr/lib/libQt5Gui.so.5.15.4 \
       /usr/lib/libQt5Core.so.5.15.4 

Im not really sure, why the vtable references are undefined. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2025-02-12 05:08:50

解决方案是,将添加到源中添加/mainwindow.hpp

add_executable(App src/mainwindow.cpp src/main.cpp include/mainwindow.hpp resources/mainwindow.ui)

这似乎是生成的 ui_mainwindow.h 看到Mainwindow类的VTable条目。

The solution to this is, adding include/mainwindow.hpp to the sources.

add_executable(App src/mainwindow.cpp src/main.cpp include/mainwindow.hpp resources/mainwindow.ui)

This seems to be required for the generated ui_mainwindow.h to see the vtable entries of the MainWindow class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文