简体   繁体   中英

QT6 windows linker error when using Q_OBJECT

I am trying to build a simple QT6 program using VSCode and MSVC. For this, I have following files:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.0)

project(test VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(test main.cpp mainwindow.cpp mainwindow.h)

find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

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

target_link_libraries(test Qt6::Core Qt6::Widgets)

main.cpp

#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow()
    : QMainWindow()
{
}

MainWindow::~MainWindow()
{
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{

    Q_OBJECT
public:

    MainWindow();

    virtual ~MainWindow();

};

#endif // MAINWINDOW_H

I am using MSCV with Kit Visual Studio Community 2019 Release - x86_amd64 . My QT6 install is under C:\Qt and my Path varable contains C:\Qt\6.2.3\msvc2019_64

As I stated in the title, when I set Q_OBJECT for the MainWindow class, I get the following linker errors:

[build] mainwindow.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UEBAPEBUQMetaObject@@XZ)". [C:\Users\Nightbird\Desktop\test\build\test.vcxproj]
[build] mainwindow.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: virtual void * __cdecl MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UEAAPEAXPEBD@Z)". [C:\Users\Nightbird\Desktop\test\build\test.vcxproj]
[build] mainwindow.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)". [C:\Users\Nightbird\Desktop\test\build\test.vcxproj]

If I remove Q_OBJECT, the project compiles / links and runs. The problem is, for me to later implement Signal/Slot-Mechanisms, I need Q_OBJECT.

Now the linker errors suggest that something goes wrong after MOC creation, however I cant find the mistake I made. Do I have to change something in my CMakeList.txt?

So it seems I found out what the problem was...

I rearranged my CMakeList.txt like so:

cmake_minimum_required(VERSION 3.10.0)

project(test VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

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

add_executable(test main.cpp mainwindow.cpp mainwindow.h)

find_package(Qt6 REQUIRED COMPONENTS Core Widgets)

target_link_libraries(test Qt6::Core Qt6::Widgets)

Notice the position of the set()-Commands... That seemed to be it. Now the output also shows [build] Automatic MOC and UIC for target test ... that didn't show before. So if you're struggling with linker errors regarding MOC, see if this line shows up. If it doesn't, check the order of cmake commands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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