LNK2005 & LNK1169 error when use QMap between 2 dlls
-
Hi, i'have an issue since 2days about an LNK2005 & LNK1169 error on MSVC 2015 (Qt 5.12.6)
My error is:
Core.lib(Core.dll) : error LNK2005: "public: __cdecl QMap<int,double>::~QMap<int,double>(void)" (??1?$QMap@HN@@QEAA@XZ) already defined in xxx.objI have the error when i'm trying to compile a library (named AAA)
The library AAA use Core.dll and both use a 2nd lib named Common.dll. The type QMap<int,double> is used in each libraries.When i'm looking xxx.obj (located in AAA), the only one usage i have of QMap<int,double>, is when i'm use a function that return a QMap and located in Core.dll
I have lot of function defined in Core and used in AAA but i never seen this error before.
I have check multiple things: trying to change the QMap with QVector, same type of error.
I don't have any "include cpp file"
I don't have the error when i'm using Clang or GCC to compile the project.
I don't have the error if i'm using a QMap<int,double>*, but i don't want toI think it's an error related to the qmap template but i'm not sure
Do you have an idea? Thanks
Ps: i'v change the name of third lib (AAA) cause this is a code i use for my work and cannot share lot of things about it
-
When i'm looking xxx.obj (located in AAA), the only one usage i have of QMap<int,double>, is when i'm use a function that return a QMap and located in Core.dll
Check if it is used in a header .h file. If yes, try to add #include <QMap> in it.
It is not good enough to include <QMap> in its .cpp file -
Hi and welcome to devnet,
How is that map declared ?
Can you share the header files ? -
Thx for your messages,
The issue is:
I have a class named Serie with a function named getOrientations that return a QMap<int, double>. Serie is located in Core.dll
I use an Serie object and the function getOrientations in another class named SortSeries (located in AAA).I have change my fonction getOrientations for returning a QMap<int, double>* (a ptr) and that work, but i don't need a ptr
QMap is included in Serie.h and SortSeries.h.
AAA and Core has already lot of other usage of QMap<int, double> (in other functions and objects) but when i want to use it between 2 libraries, i have this issue.
I'v tested to change the return type of getOrientations by "QVector<double>" but same issue.
Do Qt register the QMap template and they can be different between libraries ?
-
Can you show the header of that Series class ?
Note that unless you are doing forwarding, you need to include the header matching the class of your objects.
-
Here the .h of Serie, located in Core.dll
#ifndef SERIE_H #define SERIE_H #include <QObject> #include "core_global.h" #include <QVector> #include <QMap> class CORESHARED_EXPORT Serie : public QObject { Q_OBJECT public: Serie(QString id = ""); QMap<int, double> getOrientations(int id = 0) const; }; #endif // SERIE_HHere the .h of SortSeries, located in AAA.dll
#ifndef SORTSERIES_H #define SORTSERIES_H #include "aaa_global.h" #include "Project/Serie.h" #include <QObject> class AAASHARED_EXPORT SortSeries : public QObject { Q_OBJECT public: SortSeries(); void setSeries(const QVector<Serie *> &series); void apply(); QVector<Serie *> getSeries(); private: QVector<Serie*> _series; }; #endif // SORTSERIES_HThe function that use
getOrientationsisapply()I have removed lot of things cause this is confidential (my work). The projet contains 8 internal libraries and AAA used 4 of them. The type QMap<int, double> is used in many classes in the project but all classes that use that type include QMap (in .h if used in .h and in .cpp if used is .cpp).
That why i have that question about the template, do Qt register it in AAA and in Core and they don't match if we want to share a QMap between the 2 libraries?
-
There's nothing special with QMap, it's just a template as any other.
I am currently wondering whether you are not hitting a bug in MSVC.
Your implementation looks mostly sane, one thing you can improve is using
const String &for yourSerieconstructor. And also it builds fine with both GCC and CLANG.One thing you can do is use MSVC2017 or even 2019 since starting with 2017 they are backward compatible.
-
Pb fixed by replacing each usage of QMap<int, double> in Core and AAA by an custom object i created in a third library that used by both.
To see the pb, i have checked my dll by using Dependency Walker and i have see many definition of QMap<int, double> in Core and in another library.
But I don't know if is a problem with the compiler or not.
I will check later with another version of MSVC, I have to recompile lot of things to verify that.I have used these website too:
-
Pb fixed by replacing each usage of QMap<int, double> in Core and AAA by an custom object i created in a third library that used by both.
To see the pb, i have checked my dll by using Dependency Walker and i have see many definition of QMap<int, double> in Core and in another library.
But I don't know if is a problem with the compiler or not.
I will check later with another version of MSVC, I have to recompile lot of things to verify that.I have used these website too:
add Qt naming space to it to see if it helps.
#ifndef SERIE_H #define SERIE_H #include <QObject> #include "core_global.h" #include <QVector> #include <QMap> QT_BEGIN_NAMESPACE class CORESHARED_EXPORT Serie : public QObject { Q_OBJECT ....... }; QT_END_NAMESPACE #endif // SORTSERIES_H