Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 113101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:38:57+00:00 2026-05-11T02:38:57+00:00

This is a follow-up to Dynamic Shared Library compilation with g++ . I’m trying

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:38:58+00:00Added an answer on May 11, 2026 at 2:38 am

    myclass.h

    #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 

    myclass.cc

    #include 'myclass.h' #include <iostream>  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<<x<<endl; } 

    class_user.cc

    #include <dlfcn.h> #include <iostream> #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->DoSomething();   destroy( myClass ); } 

    On Mac OS X, compile with:

    g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so g++ class_user.cc -o class_user 

    On Linux, compile with:

    g++ -fPIC -shared myclass.cc -o myclass.so g++ class_user.cc -ldl -o class_user 

    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 create_object and destroy_object. Your main application would not need to be changed in any way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can request a new Distribution Certificate on the iPhone… May 11, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer I think this is a bit better, as the "choice"… May 11, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer If anyone is interested in the answer, I found a… May 11, 2026 at 5:04 pm

Related Questions

This is a follow-up to Dynamic Shared Library compilation with g++ . I'm trying
For so long, I've read and understood the following truths concerning web development: HTML
I am having some trouble creating a experimental 'dynamic' style website. The site is
While cross-site scripting is generally regarded as negative, I've run into several situations where
I'm working on a web application (with ASP.NET 2.0 (C#) and jQuery) In my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.