<?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[Definition der Shared Library benutzen]]></title><description><![CDATA[<p>Hallo Zusammen!</p>
<p>Ich sitze noch immer an meinem kleinen ClassLoader. Jetzt habe ich folgendes Problem: Ich habe eine Klasse samt Definitionen in dem Binary-File und auch in dem Shared Object, dass ich zur Laufzeit dynamisch einbinde. Nun erzeuge ich mir eine Instanz der Klasse (wie ich meine aus der Shared Library heraus). Rufe ich nun aber eine Member-Funktion dieser Instanz auf stellt sich heraus das es offenbar eine Instanz der Klasse/Definitionen im Binary ist.</p>
<p>Also hier mal ein paar Quelltexte. Wenn ich das übersetze und test.exe (ich arbeite mit Cygwin) ausführe wird aber 'Binary' ausgegeben und nicht wie erhofft 'Shared Object'...</p>
<p>GNUmakefile</p>
<pre><code>all:
	g++ -DDEBUG_DLL -g -Wall -shared -o shared.so shared.cpp
	g++ -g -Wall -ldl main.cpp -o test

clean:
	rm -f *.so test.exe
</code></pre>
<p>cl.hpp</p>
<pre><code class="language-cpp">#ifndef CL_HPP_
#define CL_HPP_ 1

#include &lt;iostream&gt;

class instantiator {

	public:

		static void dump() {
			#ifndef DEBUG_DLL
				std::cout &lt;&lt; &quot;Binary&quot; &lt;&lt; std::endl;
			#else
				std::cout &lt;&lt; &quot;Shared Object&quot; &lt;&lt; std::endl;
			#endif
		}

};

extern &quot;C&quot; instantiator* create_inst() { return new instantiator; }

#endif
</code></pre>
<p>shared.cpp</p>
<pre><code class="language-cpp">#include &quot;cl.hpp&quot;
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;cl.hpp&quot;
#include &lt;dlfcn.h&gt;

int main() {

	void *handle = dlopen(&quot;./shared.so&quot;, RTLD_LAZY);

	typedef instantiator* create_inst();
	create_inst* createPtr = (create_inst*)dlsym(handle, &quot;create_inst&quot;);
	instantiator* instant = createPtr();

	instant-&gt;dump();

	delete instant;

	dlclose(handle);

	return 0;
}
</code></pre>
<p>Für Hinweise und Tipps bin ich wie immer dankbar!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/218849/definition-der-shared-library-benutzen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 18:24:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/218849.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 26 Jul 2008 11:29:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Definition der Shared Library benutzen on Sat, 26 Jul 2008 14:21:28 GMT]]></title><description><![CDATA[<p>Hallo Zusammen!</p>
<p>Ich sitze noch immer an meinem kleinen ClassLoader. Jetzt habe ich folgendes Problem: Ich habe eine Klasse samt Definitionen in dem Binary-File und auch in dem Shared Object, dass ich zur Laufzeit dynamisch einbinde. Nun erzeuge ich mir eine Instanz der Klasse (wie ich meine aus der Shared Library heraus). Rufe ich nun aber eine Member-Funktion dieser Instanz auf stellt sich heraus das es offenbar eine Instanz der Klasse/Definitionen im Binary ist.</p>
<p>Also hier mal ein paar Quelltexte. Wenn ich das übersetze und test.exe (ich arbeite mit Cygwin) ausführe wird aber 'Binary' ausgegeben und nicht wie erhofft 'Shared Object'...</p>
<p>GNUmakefile</p>
<pre><code>all:
	g++ -DDEBUG_DLL -g -Wall -shared -o shared.so shared.cpp
	g++ -g -Wall -ldl main.cpp -o test

clean:
	rm -f *.so test.exe
</code></pre>
<p>cl.hpp</p>
<pre><code class="language-cpp">#ifndef CL_HPP_
#define CL_HPP_ 1

#include &lt;iostream&gt;

class instantiator {

	public:

		static void dump() {
			#ifndef DEBUG_DLL
				std::cout &lt;&lt; &quot;Binary&quot; &lt;&lt; std::endl;
			#else
				std::cout &lt;&lt; &quot;Shared Object&quot; &lt;&lt; std::endl;
			#endif
		}

};

extern &quot;C&quot; instantiator* create_inst() { return new instantiator; }

#endif
</code></pre>
<p>shared.cpp</p>
<pre><code class="language-cpp">#include &quot;cl.hpp&quot;
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;cl.hpp&quot;
#include &lt;dlfcn.h&gt;

int main() {

	void *handle = dlopen(&quot;./shared.so&quot;, RTLD_LAZY);

	typedef instantiator* create_inst();
	create_inst* createPtr = (create_inst*)dlsym(handle, &quot;create_inst&quot;);
	instantiator* instant = createPtr();

	instant-&gt;dump();

	delete instant;

	dlclose(handle);

	return 0;
}
</code></pre>
<p>Für Hinweise und Tipps bin ich wie immer dankbar!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1553946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1553946</guid><dc:creator><![CDATA[maikg]]></dc:creator><pubDate>Sat, 26 Jul 2008 14:21:28 GMT</pubDate></item></channel></rss>