Unsolved error LNK2005:... already defined in xxy.lib(xxz.obj)
-
Visual Studio (both 2017 and 2022) gives the following linker error. StackExchange link (https://stackoverflow.com/questions/15421254/already-defined-in-obj-no-double-inclusions) says:
"This is not a compiler error: the error is coming from the linker. ......The linker finds out that you have the same symbol defined multiple times in different translation units, and complains about it (it is a violation of the One Definition Rule)."
I have files qteditorfactory.cpp and qteditorfactory.h. (corresponding to qteditorfactory.obj). But where do I find the source code corresponding to Qt5Widgetsd.dll?
Or is there a better and different way to approach this problem? I have no clue where to start and fix the error. If someone can guide me, it will be of great help.
Thank you."7>Qt5Widgetsd.lib(Qt5Widgetsd.dll) : error LNK2005: "public: __cdecl QSpacerItem::QSpacerItem(int,int,enum QSizePolicy::Policy,enum QSizePolicy::Policy)" (??0QSpacerItem@@QEAA@HHW4Policy@QSizePolicy@@0@Z) already defined in qtpropertybrowserd.lib(qteditorfactory.obj)
7>Qt5Widgetsd.lib(Qt5Widgetsd.dll) : error LNK2005: "public: __cdecl QSizePolicy::QSizePolicy(enum QSizePolicy::Policy,enum QSizePolicy::Policy,enum QSizePolicy::ControlType)" (??0QSizePolicy@@QEAA@W4Policy@0@0W4ControlType@0@@Z) already defined in qtpropertybrowserd.lib(qteditorfactory.obj)"
-
@Alcor
You should not need to look at Qt sources which went into buildingQt5Widgetsd.dll
since you can just use the documentation ofQSpacerItem::QSpacerItem
/QSizePolicy::QSizePolicy
to confirm they are exactly as reported in the error message.Although you say
I have files qteditorfactory.cpp and qteditorfactory.h. (corresponding to qteditorfactory.obj).
I think you have omitted to say this is third-party and has been used to build a
qtpropertybrowserd.lib
which you are linking against, right?qteditorfactory.cpp
'#include
s the Qt headers for these.I note that source code is 8 years old. Make sure you compile it and your own source code with consistent versions of Qt and compiler flags? Test compiling and linking it against an absolutely minimal main program of your own which just calls something in that library.
-
@JonB said in error LNK2005:... already defined in xxy.lib(xxz.obj):
I think you have omitted to say this is third-party and has been used to build a qtpropertybrowserd.lib which you are linking against, right?
Yes.
@JonB said in error LNK2005:... already defined in xxy.lib(xxz.obj):
Make sure you compile it and your own source code with consistent versions of Qt and compiler flags? Test compiling and linking it against an absolutely minimal main program of your own which just calls something in that library.
Good idea. Let me try.
-
Just in case, if, this info helps in any way. I am able to separately compile and build this QtPropertyBrowser suite (modified for Qt5) using Visual Studio 2017. The generated .exe files for 7 example cases run fine.
-
@JonB said in error LNK2005:... already defined in xxy.lib(xxz.obj):
You should not need to look at Qt sources which went into building Qt5Widgetsd.dll since you can just use the documentation of QSpacerItem::QSpacerItem/QSizePolicy::QSizePolicy to confirm they are exactly as reported in the error message.
#Include statement is on line 57
#include <QSpacerItem>
On line 2183 and 2392 it's being used as follows:
lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
https://github.com/abhijitkundu/QtPropertyBrowser/blob/master/src/qteditorfactory.cpp
Which matches with the constructor as defined in Qt5 documentation
QSpacerItem::QSpacerItem(int w, int h, QSizePolicy::Policy hPolicy = QSizePolicy::Minimum, QSizePolicy::Policy vPolicy = QSizePolicy::Minimum)
-
Did you try including QSizePolicy in qteditorfactory.cpp?
#include <QSizePolicy>
-
@mustafiz said in error LNK2005:... already defined in xxy.lib(xxz.obj):
#include <QSizePolicy>
Yes. The error message persists.
-
I remove the following line
add_compile_definitions(QT_STATIC)
in the CMakelists.txt (in the library files folder, not the client folder), and the link error goes away.
Yet to figure out if my .exe works as expected.