C++ Dynamic Shared Library on Linux -


This is a follow-up.

I'm trying to create a shared class library on Linux in C ++ I am able to compile the library, and I can call some (non-class) functions using those tutorials which I found and when I try to use classes defined in the library then my problems begin. I second tutorial which shows me the link that the cell defined in the library How to load symbols for the S objects, but Using stops to reduce any work those things.

Does any shared C ++ class find more complete tutorials for creating libraries, which also shows how to use those classes have a different performance In the right? A very easy tutorial that shows the creation of objects, use (simple recipients and the setters will be fine), and deletion will be fantastic, a link or a reference to some open source code that shows the use of a shared class library, The better it will be.


While answering and working, a copy after asking this question, and a better explanation for creating and using both the code and the stable and shared libraries in its first chapter. has given. These examples are available through Google Book Search.

myclass.h

  # Ifndef __MYCLASS_H__ # defined __MYCLASS_H__ class MyClass {public: MyClass (); / * Virtual will otherwise try to linker using static linking * / Virtual Zero DoSomething (); Private: int x; }; #endif  

myclass.cc

  #include "myclass.h" #include & lt; Iostream & gt; using namespace std; Extern "C" MyClass * create_object () {Return new MyClass; } Extern "C" zero deleted_ object (MyClass * object) {delete object; } MyClass :: MyClass () {x = 20; } Zero MyClass :: DoSomething () {cout & lt; & Lt; X & lt; & Lt; Endl; }  

class_user.cc

  #include & lt; Dlfcn.h & gt; # Include & lt; Iostream & gt; # Include "myclass.h" by using the name std; Int main (int arc, four ** argv) {/ * on Linux, use "./myclass.so" * / void * handle = dlopen ("myclass.so", RTLD_LAZY); MyClass * (* create) (); Zero (* destroyed) (MyClass *); Create = (MyClass * (*) ()) dlsym (handle, "create_object"); Destroy = (Zero (*) (Mylcus *)) dlsym (handle, "deleted_object"); Create MyClass * myClass = (MyClass *) (); MyClass-> DoSomething (); Destroyed (myClass); }  

On Mac OS X, compile:

  G ++ -dynamiclib -flat_namespace myclass.cc -o myclass.so G ++ class_user Cc -o class_user  

Compile at Linux:

  G ++ - FPIC-myclass.cc -o myclass.so G ++ class_user.cc -ldl -o class_user  

If this plugin was for the system, then you would use MyCalus as a base class and define all the necessary functions virtual. The plugin will then receive author from Myclass, virtual overrides and will implement create_object and destroy_object . Your main application does not need to change in any way.


Comments