[Qt-interest] Making a Qt program into a dynamic library

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Wed Dec 16 14:28:08 CET 2009


The file QtBasedLib.dll has been removed because it violated the Swisscom security policy.
The file QtBasedLib.dll has been removed because it violated the Swisscom security policy.
The file NonQtApp.exe has been removed because it violated the Swisscom security policy.
The file mingwm10.dll has been removed because it violated the Swisscom security policy.
The file libgcc_s_dw2-1.dll has been removed because it violated the Swisscom security policy.
Guido Seifert wrote on Wednesday, December 16, 2009 12:10 PM:

> ...
> It was the 'extern "C"' which was missing. In retrospect it is
> obvious that it is necessary. Still the core file and the fact at not
> even loading the lib was possible did not make debugging easier.  

Hehe, that's funny, everyone came up with a solution already (a bit faster than me, but I had lunch in the meantime as well ;)

Anyway, I have attached my solution as well, just for the record. Since I am sitting on a Windows machine I used the Win32 equivalent calls (LoadLibrary, ...), so this gives some new input, too :).

I double-checked that my "NonQtApp" has absolutely no dependencies with Qt ("Dependency Walker") and I even linked QtGui (not just QtCore) with my simple "plugin" ("QtBasedLib"), which prints a string and shows the Qt about dialog (just to make sure that also UI stuff is working).

So here is parts of the QBasedLib.h:

class QTBASEDLIBSHARED_EXPORT QtBasedLib : public PluginInterface {
public:
    QtBasedLib();
    virtual ~QtBasedLib();

    void initialise(int argc, char**argv);
    void sayHello();
};

extern "C" {
    QTBASEDLIBSHARED_EXPORT PluginInterface *create();
}

with the extern "C" ;), and properly exported with QTBASEDLIBSHARED_EXPORT (Windows specific).

And here is the main():

typedef PluginInterface *(WINAPI *CREATE_FUNCTION)();

int main(int argc, char **argv) {
    // error handling omitted ;)
    HMODULE handle = LoadLibrary(TEXT("QtBasedLib.dll"));
    CREATE_FUNCTION create = (CREATE_FUNCTION)GetProcAddress(handle, "create");

    PluginInterface *plugin = create();
    plugin->initialise(argc, argv);
    plugin->sayHello();

    delete plugin;
    FreeLibrary(handle);
    return 0;
}

I have left the *.exe in the ZIP, but one needs to copy QtCore and QtGui into LinkTest/NonQtApp/release (as well as the actual QtBasedLib plugin - this is already the case in the ZIP) in order to run the "NonQtApp.exe". I compiled it with the latest Qt SDK (Qt Creator), hence the MinGW dependencies, based on Qt 4.6.

Note: On Windows, if you compile this with Visual Studio, make sure you compile this with "Multithreaded-DLL (/MD)" for all projects (so all DLLs/*.exe have a common address space) - otherwise

  delete plugin;

would blow up! ;)

Hope that helps anyone, too.


Cheers, Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list