[Interest] Qt5 +CLR -> linker error LNK2005

Sascha Cunz sascha-ml at babbelbox.org
Tue Feb 5 10:50:08 CET 2013

Hello Franz,

Am Montag, 4. Februar 2013, 10:49:59 schrieb Franz Hirschbeck:
> Hello,
> In one of our projects we are using managed c++ together with Qt. After
> porting the code to Qt5 the following linker error occurs. This happens
> in every combination VS2008/2010 and win32/x64.
> 1>file2.obj : error LNK2005: "public: __cdecl `public: __cdecl
> QList<class QString>::QList<class QString>(class QList<class QString>
> const &)'::`5'::Cleanup::Cleanup(struct QListData::Data *)"
> (??0Cleanup@?4???0?$QList at VQString@@@@QEAA at AEBV1@@Z at QEAA@PEAUData at QListData@
> @@Z) already defined in file1.obj
> The error is located in <QtCore/QList>.
> The "Cleanup" struct is defined, declared and instantiated within the
> templated constructor of the class QList. If it is used in 2 independent
> .cpp files with the same template parameter than it should be created in
> both objects. But the linker can see that they are identical.
> When the compiler was called with the /clr flag this results in the
> linker error afterwards.
> I tracked this down to a very simple example which is attached.
> I am not sure if this is a bug from the compiler, a limitation of clr or
> if the Qt code itself is not valid. To me the code looks correct and I
> don't know of a limitation of CLR in that case.
> Any ideas?

I've been working in a company where we used to do that kind of stuff a lot. 
But I've left them shortly before Qt 4.7 was released - so i don't have all 
the tricks I did there at hand; and I'm currently not using Windows, so I 
can't test very much.

However, I know that compiling anything with /CLR is a big challenge at times. 
In fact giving /clr to the compiler instructs it to treat everything it is fed 
as input to be compiled into managed code, including one or another implicitly 
defined "thunked" methods. In my experience, this is not what you want most of 
the time.

You can try to wrap the includes into "#pragma managed" blocks[1] in order to 
tell the compiler: "Hey, look at this. It's C++, you still know what that is, 
don't you?"

i.e. like:

#pragma managed( push, off )
#include <QtCore/QStringList>
#pragma managed( pop )


[1] http://msdn.microsoft.com/en-us/library/0adb9zxe%28v=vs.80%29.aspx

More information about the Interest mailing list