views:

81

answers:

1

Has anyone had success compiling QJson statically into an application? I am trying to use QJson statically in my Qt application (Windows/Mac), meaning I'm trying to use the source files directly rather than compiling a DLL and using it. Is this possible? My program is producing lots of errors when I attempt to do it, mostly "multiple declaration" errors. They are seemingly related to having a method structure like this:

SerializerRunnable::SerializerRunnable(QObject* parent)
    : QObject(parent),
      QRunnable(),
      d(new Private)
{
  qRegisterMetaType<QVariant>("QVariant");
}
SerializerRunnable::~SerializerRunnable()
{
  delete d;
}

Any ideas would be appreciated.

Thanks,

+2  A: 

Code that's compiled into a DLL needs to export the functions and classes that it wants to expose to the outside world linking to it at runtime.

In this particular case that magic happens in qjson_export.h:

#ifndef QJSON_EXPORT_H
#define QJSON_EXPORT_H

#include <QtCore/qglobal.h>

#ifndef QJSON_EXPORT
# if defined(QJSON_MAKEDLL)
   /* We are building this library */
#  define QJSON_EXPORT Q_DECL_EXPORT
# else
   /* We are using this library */
#  define QJSON_EXPORT Q_DECL_IMPORT
# endif
#endif

#endif

If you don't have DEFINES += QJSON_MAKEDLL in your .pro file then the compiler assumes that you are using a DLL, rather than compiling code, and gets confused when code that is marked as "defined elsewhere" by Q_DECL_EXPORT is, in fact, right there, and stupidly assumes it's being defined multiple times.

I hope that makes sense. :P

kurige
Ahh that works! Thank you very much. I was confused by QJSON_MAKEDLL and assumed that it would be making a DLL rather than compiling source into my result executable. I'm curious why you only need to define QJSON_MAKEDLL for Windows? Is it not also a shared library by default on Unix based systems?
jocull
I can't speak to this with any authority, but I would guess that it has something to do with the fact that all functions/classes/etc are public by default when building a shared library on Unix. But that doesn't explain why it would "work" when `Q_DECL_IMPORT` is clearly and explicitly declared in the absence of a `QJSON_MAKEDLL` when building the `.pro` file in a Unix environment... Maybe somebody else can shed some light on this.
kurige