This is a follow-up to Dynamic Shared Library compilation with g++. I'm trying to create a shared class library in C++ on Linux. I'm able to get the library to compile, and I can call some of the (non-class) functions using the tutorials that I found here and here. My problems start when I try to use the classes that are defined in the library. The second tutorial that I linked to shows how to load the symbols for creating objects of the classes defined in the library, but stops short of using those objects to get any work done. Does anyone know of a more complete tutorial for creating shared C++ class libraries that also shows how to use those classes in a separate executable? A very simple tutorial that shows object creation, use (simple getters and setters would be fine), and deletion would be fantastic. A link or a reference to some open source code that illustrates the use of a shared class library would be equally good. Although the answers from codelogic and nimrodm do work, I just wanted to add that I picked up a copy of Beginning Linux Programming since asking this question, and its first chapter has example C code and good explanations for creating and using both static and shared libraries. These examples are available through Google Book Search in an older edition of that book.
1 1 1 silver badge asked Jan 30, 2009 at 18:41 Bill the Lizard Bill the Lizard 404k 210 210 gold badges 572 572 silver badges 888 888 bronze badgesI'm not sure I understand what you mean by "using" it, once a pointer to the object is returned, you could use it like you use any other pointer to an object.
Commented Jan 30, 2009 at 19:41The article I linked to shows how to create a function pointer to an object factory function using dlsym. It doesn't show the syntax for creating and using objects from the library.
Commented Jan 30, 2009 at 19:54You will need the header file describing the class. Why do you think you have to use "dlsym" instead of just letting the OS find and link the library at load time? Let me know if you need a simple example.
Commented Jan 30, 2009 at 20:00@nimrodm: What's the alternative to using "dlsym"? I'm (supposed to be) writing 3 C++ programs that will all use the classes defined in the shared library. I also have 1 Perl script that will use it, but that's a whole other problem for next week.