<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] How can a plugin that is dynamically loadded access main application objects and data?]]></title><description><![CDATA[<p dir="auto">Hi there</p>
<p dir="auto">I have an application that loads plugins in run time.  I want  to write a singleton pattern (and also later Factory pattern) and able to access from loadded plugins.  I added header files for singleton class to plugin project. But when I try load plugins in main application, I get undefined symbol error and library can't be loadded. If I add implementation file(singleton.cpp) to plugin , loadded plugin use own singleton code not the main application. I prepared simple program to explain my problem. You can see the codes under.</p>
<p dir="auto">loadplugin main program Project File<br />
@<br />
QT       += core</p>
<p dir="auto">QT       -= gui</p>
<p dir="auto">TARGET = loadplugin<br />
CONFIG   += console<br />
CONFIG   -= app_bundle</p>
<p dir="auto">TEMPLATE = app</p>
<p dir="auto">SOURCES += main.cpp <br />
singleton.cpp</p>
<p dir="auto">HEADERS += <br />
moduleinterface.h <br />
singleton.h<br />
@</p>
<p dir="auto">Main Application Singleton header<br />
@<br />
#ifndef SINGLETON_H<br />
#define SINGLETON_H<br />
class Singleton<br />
{<br />
static Singleton* sInstance;<br />
public:<br />
int property;<br />
Singleton(){property=1; Singleton::sInstance=0;}<br />
static Singleton&amp; instance();<br />
~Singleton();<br />
};</p>
<p dir="auto">#endif // SINGLETON_H</p>
<p dir="auto">@</p>
<p dir="auto">Main application singleton implementation file<br />
@<br />
#include "singleton.h"<br />
Singleton* Singleton::sInstance=0;</p>
<p dir="auto">Singleton::~Singleton()<br />
{</p>
<p dir="auto">}<br />
Singleton&amp; Singleton::instance()<br />
{<br />
if(sInstance == 0)<br />
{<br />
sInstance = new Singleton();<br />
}<br />
return *(sInstance);<br />
}</p>
<p dir="auto">@</p>
<p dir="auto">Main application main.cpp file<br />
@<br />
#include &lt;QCoreApplication&gt;<br />
#include &lt;QPluginLoader&gt;<br />
#include &lt;QDebug&gt;<br />
#include &lt;moduleinterface.h&gt;<br />
#include &lt;singleton.h&gt;</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QCoreApplication a(argc, argv);</p>
<pre><code>QString moduleName = "testplugin";
QString functionName = "command";
Singleton::instance().property=3;
            const QString *libPath = new QString("/home/ckurdu/Documents/my_works/C++/qt/qtbin/qtcms_modules/"+moduleName+"/libqt"+moduleName+".so");
            QPluginLoader pluginLoader(*libPath);
            pluginLoader.load();
            if(pluginLoader.isLoaded())
            {
            QObject *plugin = pluginLoader.instance();
                if(plugin)
                {
                   ModuleInterface *moduleInt = qobject_cast&lt;ModuleInterface *&gt;(plugin);
                    if(moduleInt)
                    {
                       qDebug()&lt;&lt;"Module loaded"&lt;&lt;endl;
                       qDebug()&lt;&lt;moduleInt-&gt;execute(functionName);
                    }else{
                        qDebug()&lt;&lt;"Module not converted"&lt;&lt;endl;
                    }
                }else{
                    qDebug()&lt;&lt;"Module couldn't resolve"&lt;&lt;endl;
                }

            }else{
                if(!QLibrary::isLibrary(*libPath))
                {
                   qDebug()&lt;&lt;"File is not a type of library" &lt;&lt;endl;
                }
                qDebug()&lt;&lt;"Module not loaded"&lt;&lt;endl;
                qDebug()&lt;&lt;pluginLoader.errorString()&lt;&lt;endl;
            }

return a.exec&amp;#40;&amp;#41;;
</code></pre>
<p dir="auto">}</p>
<p dir="auto">@</p>
<p dir="auto">moduleinterface.h  file for plugin creation.<br />
@<br />
#ifndef MODULEINTERFACE_H<br />
#define MODULEINTERFACE_H<br />
#include &lt;QString&gt;<br />
#include &lt;QObject&gt;<br />
class ModuleInterface<br />
{<br />
public:<br />
virtual ~ModuleInterface() {}<br />
virtual QString display(QString) = 0;<br />
virtual QString execute(QString) = 0;<br />
};</p>
<p dir="auto">QT_BEGIN_NAMESPACE</p>
<p dir="auto">#define ModuleInterface_iid "proje.tc.ModuleInterface"</p>
<p dir="auto">Q_DECLARE_INTERFACE(ModuleInterface, ModuleInterface_iid)</p>
<p dir="auto">QT_END_NAMESPACE</p>
<p dir="auto">#endif // MODULEINTERFACE_H</p>
<p dir="auto">@<br />
*<br />
Example plugin TestPlugin pro file.<br />
*<br />
@<br />
QT       -= gui<br />
CONFIG         += plugin</p>
<p dir="auto">TARGET = TestPlugin<br />
TEMPLATE = lib</p>
<p dir="auto">DEFINES += TESTPLUGIN_LIBRARY</p>
<p dir="auto">SOURCES += testplugin.cpp <br />
../../loadplugintest/loadplugin/singleton.cpp</p>
<p dir="auto">HEADERS += testplugin.h<br />
testplugin_global.h <br />
../../loadplugintest/loadplugin/moduleinterface.h <br />
../../loadplugintest/loadplugin/singleton.h</p>
<p dir="auto">TARGET          = $$qtLibraryTarget(qttestplugin)<br />
DESTDIR = /home/ckurdu/Documents/my_works/C++/qt/qtbin/qtcms_modules/testplugin/</p>
<p dir="auto">@</p>
<p dir="auto">plugin header file</p>
<p dir="auto">@<br />
#ifndef TESTPLUGIN_H<br />
#define TESTPLUGIN_H</p>
<p dir="auto">#include &lt;QObject&gt;<br />
#include &lt;QtPlugin&gt;<br />
#include &lt;QString&gt;<br />
#include  "../../loadplugintest/loadplugin/moduleinterface.h"<br />
#include  "../../loadplugintest/loadplugin/singleton.h"</p>
<p dir="auto">class TestPlugin : public QObject, ModuleInterface<br />
{<br />
Q_OBJECT<br />
Q_PLUGIN_METADATA(IID "proje.tc.ModuleInterface")<br />
Q_INTERFACES(ModuleInterface)<br />
int yesno=false;<br />
public:<br />
QString display(QString command);<br />
QString execute(QString command);<br />
TestPlugin(QObject *parent=NULL);<br />
};</p>
<p dir="auto">#endif // TESTPLUGIN_H</p>
<p dir="auto">@</p>
<p dir="auto">TestPlugin implementation<br />
@<br />
#include "testplugin.h"<br />
#include &lt;QDebug&gt;</p>
<p dir="auto">TestPlugin::TestPlugin(QObject* parent):QObject(parent)<br />
{<br />
}</p>
<p dir="auto">QString TestPlugin::display(QString command)<br />
{<br />
return command;<br />
}</p>
<p dir="auto">QString TestPlugin::execute(QString command)<br />
{<br />
qDebug()&lt;&lt;Singleton::instance().property&lt;&lt;endl;<br />
return command;<br />
}</p>
<p dir="auto">@</p>
<p dir="auto">If I add singleton.cpp file to my simple test plugin I get singleton::property value as 1. If singleton code of main program runs, singleton::property value must be 3.</p>
<p dir="auto">Sorry for my english.</p>
]]></description><link>https://forum.qt.io/topic/49768/solved-how-can-a-plugin-that-is-dynamically-loadded-access-main-application-objects-and-data</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 16:39:47 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/49768.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 02 Jan 2015 19:00:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 21:13:22 GMT]]></title><description><![CDATA[<p dir="auto">Sağolasın :)</p>
]]></description><link>https://forum.qt.io/post/256119</link><guid isPermaLink="true">https://forum.qt.io/post/256119</guid><dc:creator><![CDATA[CKurdu]]></dc:creator><pubDate>Sat, 03 Jan 2015 21:13:22 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 20:48:06 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="CKurdu" date="1420317414"]It worked perfectly. Thank you. Which method do you suggest?</p>
<p dir="auto">1 .  Making a library for factory methods and load this librarry into main application and for each plugins.</p>
<ol start="2">
<li>Your solution.</li>
</ol>
<p dir="auto">[/quote]</p>
<p dir="auto">I would suggest my solution. Not because it is my solution :), but because you'd need to take care of one library less.</p>
<p dir="auto">Kolay gelsin.</p>
]]></description><link>https://forum.qt.io/post/256120</link><guid isPermaLink="true">https://forum.qt.io/post/256120</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Sat, 03 Jan 2015 20:48:06 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 20:36:54 GMT]]></title><description><![CDATA[<p dir="auto">It worked perfectly. Thank you. Which method do you suggest?</p>
<p dir="auto">1 .  Making a library for factory methods and load this librarry into main application and for each plugins.</p>
<ol start="2">
<li>Your solution.</li>
</ol>
]]></description><link>https://forum.qt.io/post/256118</link><guid isPermaLink="true">https://forum.qt.io/post/256118</guid><dc:creator><![CDATA[CKurdu]]></dc:creator><pubDate>Sat, 03 Jan 2015 20:36:54 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 19:14:13 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">The point of using an interface is that you don't need to link to a concrete implementation. You should not add a non-virtual method to an interface. Also you shouldn't have a, static or not, a data member in an interface.</p>
<p dir="auto">You are calling the static instance() member on the SingletonInterface pointer you've passed to the plugin. This is the problem. You shouldn't do that. Just call the non-static virtual members via the pointer.</p>
<p dir="auto">To make it clearer, look at this line of code in the TestPlugin<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/qdebug">@<bdi>qDebug</bdi></a>()&lt;&lt;this-&gt;psing-&gt;instance().property&lt;&lt;endl;@</p>
<p dir="auto">Here you are trying to access a concrete member. Of course the runtime will complain because it needs to read the "property" directly. You should have used the virtual getProperty() accessor:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/qdebug">@<bdi>qDebug</bdi></a>()&lt;&lt;this-&gt;psing-&gt;getProperty()&lt;&lt;endl;@</p>
<p dir="auto">Put the static instance() member and the static property data member into Singleton. This way, you won't need to dynamic_cast and your plugin won't need a concrete SingletonInterface implementation.</p>
]]></description><link>https://forum.qt.io/post/256111</link><guid isPermaLink="true">https://forum.qt.io/post/256111</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Sat, 03 Jan 2015 19:14:13 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 18:47:38 GMT]]></title><description><![CDATA[<p dir="auto">bq. definition of the singleton interface class<br />
@<br />
#ifndef SINGLETONINTERFACE<br />
#define SINGLETONINTERFACE<br />
class SingletonInterface<br />
{<br />
public:<br />
static int property;<br />
virtual int getProperty() = 0;<br />
virtual void setProperty(int value) = 0;<br />
static SingletonInterface&amp;  instance();<br />
};<br />
#endif // SINGLETONINTERFACE<br />
@</p>
<p dir="auto">bq. new definition of the Singleton class</p>
<p dir="auto">@#ifndef SINGLETON_H<br />
#define SINGLETON_H<br />
#include "singletoninterface.h"<br />
class Singleton:public SingletonInterface<br />
{<br />
static Singleton* sInstance;<br />
public:<br />
int property;<br />
Singleton(){property=1; Singleton::sInstance=0;}<br />
static SingletonInterface&amp; instance();<br />
int getProperty(){ return this-&gt;property;}<br />
void setProperty(int value){this-&gt;property = value;}<br />
~Singleton();<br />
};</p>
<p dir="auto">#endif // SINGLETON_H@</p>
<p dir="auto">bq. new definition of the plugin interface</p>
<p dir="auto">@#ifndef MODULEINTERFACE_H<br />
#define MODULEINTERFACE_H<br />
#include &lt;QString&gt;<br />
#include &lt;QObject&gt;<br />
#include "/home/ckurdu/Documents/my_works/C++/qt/tests/loadplugintest/loadplugin/singletoninterface.h"<br />
class ModuleInterface<br />
{<br />
public:<br />
virtual ~ModuleInterface() {}<br />
virtual QString display(QString) = 0;<br />
virtual QString execute(QString) = 0;<br />
virtual void setSingleton(SingletonInterface *ptr) =0;<br />
};</p>
<p dir="auto">QT_BEGIN_NAMESPACE</p>
<p dir="auto">#define ModuleInterface_iid "proje.tc.ModuleInterface"</p>
<p dir="auto">Q_DECLARE_INTERFACE(ModuleInterface, ModuleInterface_iid)</p>
<p dir="auto">QT_END_NAMESPACE</p>
<p dir="auto">#endif // MODULEINTERFACE_H@</p>
<p dir="auto">bq. implementation of the setSingleton() member of the concrete plugin class implementing the plugin interface</p>
<p dir="auto">@#include "testplugin.h"<br />
#include &lt;QDebug&gt;</p>
<p dir="auto">TestPlugin::TestPlugin(QObject* parent):QObject(parent)<br />
{<br />
}</p>
<p dir="auto">QString TestPlugin::display(QString command)<br />
{<br />
return command;<br />
}</p>
<p dir="auto">QString TestPlugin::execute(QString command)<br />
{<br />
qDebug()&lt;&lt;this-&gt;psing-&gt;instance().property&lt;&lt;endl;<br />
return command;<br />
}</p>
<p dir="auto">void TestPlugin::setSingleton(SingletonInterface *ptr)<br />
{<br />
this-&gt;psing = ptr;<br />
}@</p>
<p dir="auto">@the code segment where you call the setSingleton() on the plugin@</p>
<p dir="auto">@#include &lt;QCoreApplication&gt;<br />
#include &lt;QPluginLoader&gt;<br />
#include &lt;QDebug&gt;<br />
#include &lt;moduleinterface.h&gt;<br />
#include &lt;singleton.h&gt;</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QCoreApplication a(argc, argv);</p>
<pre><code>QString moduleName = "testplugin";
QString functionName = "command";
Singleton &amp;singleton = dynamic_cast&lt;Singleton&amp;&gt;(Singleton::instance());
          singleton.property=3;

            const QString *libPath = new QString("/home/ckurdu/Documents/my_works/C++/qt/qtbin/qtcms_modules/"+moduleName+"/libqt"+moduleName+".so");
            QPluginLoader pluginLoader(*libPath);
            pluginLoader.load();
            if(pluginLoader.isLoaded())
            {
            QObject *plugin = pluginLoader.instance();
                if(plugin)
                {
                   ModuleInterface *moduleInt = qobject_cast&lt;ModuleInterface *&gt;(plugin);
                    if(moduleInt)
                    {
                       qDebug()&lt;&lt;"Module loaded"&lt;&lt;endl;
                       moduleInt-&gt;setSingleton(&amp;singleton);
                       qDebug()&lt;&lt;moduleInt-&gt;execute(functionName);
                    }else{
                        qDebug()&lt;&lt;"Module not converted"&lt;&lt;endl;
                    }
                }else{
                    qDebug()&lt;&lt;"Module couldn't resolve"&lt;&lt;endl;
                }

            }else{
                if(!QLibrary::isLibrary(*libPath))
                {
                   qDebug()&lt;&lt;"File is not a type of library" &lt;&lt;endl;
                }
                qDebug()&lt;&lt;"Module not loaded"&lt;&lt;endl;
                qDebug()&lt;&lt;pluginLoader.errorString()&lt;&lt;endl;
            }

return a.exec&amp;#40;&amp;#41;;
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">setSingleton is called on Line 28 . But program never reachs this line.</p>
<p dir="auto">Error Message<br />
"Cannot load library /home..../libqttestplugin.so : undefined symbol: _Zn18SingletonInterface8propertyE"</p>
<p dir="auto">I also solved the problem with different way.<br />
I created a plugin that have singleton and factory methods. I load this plugin from main and testplugin. It works. I don't understand how but<br />
library have same data between main and loadded testplugin.</p>
]]></description><link>https://forum.qt.io/post/256109</link><guid isPermaLink="true">https://forum.qt.io/post/256109</guid><dc:creator><![CDATA[CKurdu]]></dc:creator><pubDate>Sat, 03 Jan 2015 18:47:38 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 09:36:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi again,</p>
<p dir="auto">Unfortunately I couldn't understand where and how the error occurs. Pure abstract classes completely decouple declarations and implementations therefore I wouldn't expect much issues. Moreover an error like "undefined symbol" smells like a missing header include somewhere.</p>
<p dir="auto">Could you please post here the following listings?</p>
<ul>
<li>definition of the singleton interface class</li>
<li>new definition of the Singleton class</li>
<li>new definition of the plugin interface</li>
<li>implementation of the setSingleton() member of the concrete plugin class implementing the plugin interface</li>
<li>the code segment where you call the setSingleton() on the plugin</li>
</ul>
]]></description><link>https://forum.qt.io/post/256081</link><guid isPermaLink="true">https://forum.qt.io/post/256081</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Sat, 03 Jan 2015 09:36:03 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Sat, 03 Jan 2015 08:39:04 GMT]]></title><description><![CDATA[<p dir="auto">Hi ckakman</p>
<p dir="auto">I tried with abstract class. I get error message when Main application loads the library. "undefined symbol". If I remove the line "this-&gt;singleton = singleton" in setSingleton method of plugin, Program loads plugin (library) successfully. But of course it is not a solution.</p>
]]></description><link>https://forum.qt.io/post/256078</link><guid isPermaLink="true">https://forum.qt.io/post/256078</guid><dc:creator><![CDATA[CKurdu]]></dc:creator><pubDate>Sat, 03 Jan 2015 08:39:04 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How can a plugin that is dynamically loadded access main application objects and data? on Fri, 02 Jan 2015 22:23:53 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You may consider defining an interface, e.g. AbstractSingleton, for the singleton and derive your Sİngleton class from this interface. You can add a setter function to your plugin interface that takes a pointer or reference to an AbstractSingleton, e.g<br />
@void setSingleton(AbstractSingleton *singleton)@</p>
]]></description><link>https://forum.qt.io/post/256057</link><guid isPermaLink="true">https://forum.qt.io/post/256057</guid><dc:creator><![CDATA[ckakman]]></dc:creator><pubDate>Fri, 02 Jan 2015 22:23:53 GMT</pubDate></item></channel></rss>