If I try to compile a simple Qt project, I receive 6 linking errors:
mocs_compilation_Release.obj : error LNK2005: "private: static void __cdecl MainWindow::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@MainWindow@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) già definito in moc_mainwindow.obj
1>mocs_compilation_Release.obj : error LNK2005: "public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UEBAPEBUQMetaObject@@XZ) già definito in moc_mainwindow.obj
1>mocs_compilation_Release.obj : error LNK2005: "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) già definito in moc_mainwindow.obj
1>mocs_compilation_Release.obj : error LNK2005: "public: virtual void * __cdecl MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UEAAPEAXPEBD@Z) già definito in moc_mainwindow.obj
1>mocs_compilation_Release.obj : error LNK2005: "public: static struct QMetaObject const MainWindow::staticMetaObject" (?staticMetaObject@MainWindow@@2UQMetaObject@@B) già definito in moc_mainwindow.obj
1>E:\C++project\QT\GraphingCalculator\VSProject\Release\Calculator.exe : fatal error LNK1169: rilevato uno o più simboli definiti più volte
If I see the folder where .obj
files are generated, they are there; for example, in the release folder, there are 2 files, which I presume contain the same information:
moc_mainwindow.obj
moc_mainwindow_relase.obj
I use:
I tried to delete all .obj
files and rebuild, but nothing changed.
CMake:
cmake_minimum_required(VERSION 3.13)
project(GraphingCalculator)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
file(GLOB resource *.cpp *.h *.c *. hpp *.qrc *.ui)
find_package(Qt6widgets REQUIRED)
add_executable(Calculator ${resource})
target_link_libraries(Calculator Qt6::Widgets)
main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
(new MainWindow())->show();
return app.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QApplication>
#include <QWidget>
#include <Qpushbutton>
class MainWindow : public QWidget
{
Q_OBJECT
private:
QPushButton* button;
public:
MainWindow(QWidget* parent = 0);
};
#endif
mainwindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget* parent) : QWidget(0)
{
button = new QPushButton("Close", this);
QObject::connect(button, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
setFixedSize(100, 100);
}
There's also a .qrc
file, but it is empty.
I solve the problem using a differnet cmake file:
cmake_minimum_required(VERSION 3.16)
project(Graphing_Calculator, VERSION 0.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
file(GLOB resource *.cpp *.h *.c *. hpp *.qrc *.ui)
qt_add_executable(Graphing_Calculator ${resource})
target_link_libraries(Graphing_Calculator PRIVATE Qt6::Widgets)
set_target_properties(Graphing_Calculator PROPERTIES
WIN32_EXECUTABLE ON
)
I don't know why but in this mode all work goods!