Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>myclass.h</strong></p> <pre><code>#ifndef __MYCLASS_H__ #define __MYCLASS_H__ class MyClass { public: MyClass(); /* use virtual otherwise linker will try to perform static linkage */ virtual void DoSomething(); private: int x; }; #endif </code></pre> <p><strong>myclass.cc</strong></p> <pre><code>#include "myclass.h" #include &lt;iostream&gt; using namespace std; extern "C" MyClass* create_object() { return new MyClass; } extern "C" void destroy_object( MyClass* object ) { delete object; } MyClass::MyClass() { x = 20; } void MyClass::DoSomething() { cout&lt;&lt;x&lt;&lt;endl; } </code></pre> <p><strong>class_user.cc</strong></p> <pre><code>#include &lt;dlfcn.h&gt; #include &lt;iostream&gt; #include "myclass.h" using namespace std; int main(int argc, char **argv) { /* on Linux, use "./myclass.so" */ void* handle = dlopen("myclass.so", RTLD_LAZY); MyClass* (*create)(); void (*destroy)(MyClass*); create = (MyClass* (*)())dlsym(handle, "create_object"); destroy = (void (*)(MyClass*))dlsym(handle, "destroy_object"); MyClass* myClass = (MyClass*)create(); myClass-&gt;DoSomething(); destroy( myClass ); } </code></pre> <p>On Mac OS X, compile with:</p> <pre><code>g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so g++ class_user.cc -o class_user </code></pre> <p>On Linux, compile with:</p> <pre><code>g++ -fPIC -shared myclass.cc -o myclass.so g++ class_user.cc -ldl -o class_user </code></pre> <p>If this were for a plugin system, you would use MyClass as a base class and define all the required functions virtual. The plugin author would then derive from MyClass, override the virtuals and implement <code>create_object</code> and <code>destroy_object</code>. Your main application would not need to be changed in any way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload