I am having a serious issue with my DLL in Qt. When I compile my most recent additions (assertInt() and randomInt()), i get the following compile error
error: function ‘T randomInteger(const T&, const T&)’ definition is marked dllimport
Obviously I can force the program to compile by replacing JECLIBRARYSHARED_EXPORT with Q_DECL_EXPORT but then I would have to change it back to import when using the library. That would be a pain.
Does anyone know why these two particular subroutines are being flagged as DLLIMPORT instead of export? The rest of the library exports as it should. I have defined JECLIBRARYSHARED in my .pro file.
This is the how QtCreator generated the DLL exporting code when i created the DLL project.
JecLibary_global.h
#ifndef JECLIBRARY_GLOBAL_H
#define JECLIBRARY_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(JECLIBRARY_LIBRARY)
# define JECLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
# define JECLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // JECLIBRARY_GLOBAL_H
JecMath.h
#ifndef JECMATH_H
#define JECMATH_H
#include <QList>
#include <QTime>
#include <JecLibrary_global.h>
#include <JecUtils.h>
template<class T>
T JECLIBRARYSHARED_EXPORT randomInteger(const T& max, const T& min = 0)
{
assertInteger(max);
qsrand((uint)QTime::currentTime().msec());
return qrand() % ((max + 1) - min) + min;
}
JecUtils.h
#include <typeinfo>
#include <QString>
#include <QRegExp>
#include <JecLibrary_global.h>
template<class T>
void JECLIBRARYSHARED_EXPORT assertInteger(const T& var)
{
static_assert(sizeof(T) != sizeof(bool) ||
sizeof(T) != sizeof(char) ||
sizeof(T) != sizeof(short) ||
sizeof(T) != sizeof(int) ||
sizeof(T) != sizeof(long), "T is not an integer.");
}
The only things different about these subroutine is that they are templated. I’m also wondering if there is a dependency issue because JecMath.h includes JecUtil.h so JecLibrary_global.h gets included twice but I thought that the #Ifndef would protect against that. I’ll try some experimenting and post any results i may get.
Any help will be appreciated. Thank you,
Jec
How are you going to import or export
templatefunction? Template function has no any code to export. Caller first provides actual definition for template function, then code for that actual definition is generated.So, you cannot export
When you use this function as
foo(intValue);you provide actual definition for the template and then code forvoid foo(int param)is generated, but there is no any way to generate code until you know what istypename Tactually.Actually as the function is defined in header, you shouldn’t mark it neither
dllexportnordllimportAlso you cannot
dllimporta function, that has a defenition (not declaration), obviously.