[Qt-interest] Making a Qt program into a dynamic library
Konrad Rosenbaum
konrad at silmor.de
Wed Dec 16 11:20:30 CET 2009
On Wednesday 16 December 2009 10:36:06 Guido Seifert wrote:
> > It seems that upon initialisation of Qt - once the shared library gets
> > loaded - fails, for whatever reasons. Before the path of execution
> > reaches a single line of the OP's own code (linking with CONFIG +=
> > Qt alone is apparently enough to even make a single printf("Hello
> > World!"); fail)
>
> True. Below is a backtrace of a segfault. As it can be seen, it happens in
> QtCore. The program which caused this crash:
Strange. A minimal example works for me. See below.
So the remaining conclusion is that your "plugin" has global variables that
depend on Qt and are initialized before your main function gets called.
Fortunately this is something that you have full control over.
I recommend splitting the plugin into two plugins: one to initialize the Qt
subsystem (instantiates QApplication), which gets called first. And another
one that contains your real code and gets only loaded after Qt is
initialized.
Konrad
Minimal code:
--main.c (C-stub that loads my reall app)--
#include <dlfcn.h>
typedef int(*mft)(int,char**);
int main(int argc,char**argv)
{
void *hdl=dlopen("./nomain.so",RTLD_LAZY);
mft mf=(mft)dlsym(hdl,"nomain");
if(!mf)return 1;
return mf(argc,argv);
}
----
--nomain.cpp (Qt based app as .so file)--
#include <QApplication>
#include <QPushButton>
extern "C"
int nomain(int argc,char**argv)
{
QApplication app(argc,argv);
QPushButton p("Hello World");
p.connect(&p,SIGNAL(clicked()),&app,SLOT(quit()));
p.show();
return app.exec();
}
----
--Makefile (for Debian; creates both binaries; correct the -I directives for
your system)--
all: qx nomain.so
qx: main.c
gcc -o $@ $^ -ldl
nomain.so: nomain.cpp
g++ -o $@ $^ \
-I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore \
-I/usr/include/qt4/Qt -I/usr/include/qt4 \
-lQtCore -lQtGui -shared -fPIC
----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091216/53cf8deb/attachment.bin
More information about the Qt-interest-old
mailing list