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 8665147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:24:49+00:00 2026-06-12T17:24:49+00:00

I have an issue when I try to use dlopen() to load a shared

  • 0

I have an issue when I try to use dlopen() to load a shared library into another shared library. I checked all tutorials on how to use dlopen() correctly. So here is the simplified code:

The main shared library contains a class with pure virtual functions which the sub-shared library (aka plug-in) must implement. Additionally, it has some other functions which are implemented with a default behavior. I created a macro which is added to every plug-in to have a symbol to load and create the class.

Main shared library

plugin.h:

Class A {
public:
  virtual int func1() = 0;
  virtual bool func2() const;
}

#define CREATE_CLASS(cls) extern "C" void *CreateClass(void) { return new cls; }

plugin.cpp:

bool A::func2() const {   return true; }

Build and link main.so

g++ -g -Wall -Woverloaded-virtual -Wno-parentheses -O2 -fPIC -c -o plugin.o plugin.cpp
g++ -g -Wall -Woverloaded-virtual -Wno-parentheses -O2 -fPIC  -rdynamic -shared plugin.o -ldl -o main-plugin.so

The sub-shared library does only implement the pure-virtual functions. The other function can be overridden though it is optional.

Sub shared library

plugin-impl.cpp

Class B : public A {
public:
  virtual int func1() { return 0; }
}

CREATE_CLASS(B)

These are the lines to build and link the sub-shared library.

Build and link sub.so

g++ -g -O3 -Wall -Werror=overloaded-virtual -Wno-parentheses -fPIC -c -o subPlugin.o subPlugin.cpp
g++ -g -O3 -Wall -Werror=overloaded-virtual -Wno-parentheses -fPIC  -shared subPlugin.o  -o subPlugin.so

This is the line to open the sub-shared library. I tried LAZY, NOW, NOW | GLOBAL and so on, without any effects.

dlopen() call somewhere in the main-shared library:

  handle = dlopen(file.c_str(), RTLD_LAZY);

Most of this is working very well. However, when I try to load the sub-shared library into the main shared library, dlopen complains about the undefined symbol of bool A::func2() const. The function does only exists in the main shared library, so I guess it must be exported anyway. Please help me out! I am very confused!

SOLUTION

Because I cannot change the executable, I have to link the main-shared library with the sub-shared librarys by adding the following options to g++:

-L$(PLUGINDIR) -Wl,-R$(PLUGINDIR) -lmain-shared

With this set, it is not required to set the LD_LIBRARY_PATH.

  • 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. Editorial Team
    Editorial Team
    2026-06-12T17:24:50+00:00Added an answer on June 12, 2026 at 5:24 pm

    Since your sub-library needs symbols from the main library, I think it you want it to be linked with it. Try linking it like this:

    g++ -g -O3 -Wall -Werror=overloaded-virtual -Wno-parentheses -fPIC  -shared 
        -lmain-plugin subPlugin.o  -o subPlugin.so
    

    Probably you’ll need to play with -L as well.

    This is what I tried:

    jirka@debian:/tmp$ cat executable.cpp 
    #include <dlfcn.h>
    #include <stdio.h>
    int main()
    {
      dlopen("./main-library.so", RTLD_NOW);
      void* handle=dlopen("./sub-library.so", RTLD_LAZY);
      printf("%x %s", dlsym(handle, "CreateClass"), dlerror());
    }
    jirka@debian:/tmp$ cat main-library.cpp 
    class A {
    public:
      virtual int func1() = 0;
      virtual bool func2() const;
    };
    
    #define CREATE_CLASS(cls) extern "C" void *CreateClass(void) { return new cls; }
    
    bool A::func2() const {   return true; }
    jirka@debian:/tmp$ cat sub-library.cpp 
    class A {
    public:
      virtual int func1() = 0;
      virtual bool func2() const;
    };
    
    #define CREATE_CLASS(cls) extern "C" void *CreateClass(void) { return new cls; }
    
    class B : public A {
    public:
      virtual int func1() { return 0; }
    };
    
    CREATE_CLASS(B)
    
    jirka@debian:/tmp$ g++ -g -Wall -Woverloaded-virtual -Wno-parentheses -O2 -fPIC  -rdynamic -shared main-library.cpp -ldl -o main-library.so
    jirka@debian:/tmp$ g++ -g -O3 -Wall -Werror=overloaded-virtual -Wno-parentheses -fPIC  -shared -l:main-library.so sub-library.cpp  -o sub-library.so
    jirka@debian:/tmp$  g++ -ldl executable.cpp -o executable
    jirka@debian:/tmp$ LD_LIBRARY_PATH=. ./executable 
    b7713740 (null)
    

    Another possibility is adding RTLD_GLOBAL when loading main-library:

    jirka@debian:/tmp$ cat executable.cpp 
    #include <dlfcn.h>
    #include <stdio.h>
    int main()
    {
      dlopen("./main-library.so", RTLD_LAZY | RTLD_GLOBAL);
      void* handle=dlopen("./sub-library.so", RTLD_LAZY);
      printf("%x %s", dlsym(handle, "CreateClass"), dlerror());
    }
    

    That way, you needn’t link anything with main-library.so.

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

Sidebar

Related Questions

Yes I have a issue when i try to use bindvalues on the variables
I have an issue to try to use a field defined as a decimal(17)
I have an issue when I try to use datagrid.items.add() to add items from
I try to migrate to GCM and I have an issue with the SENDER_ID
I have a problem and I will try to explain the issue: I have
I have a fun issue where during application shutdown, try / catch blocks are
I have a very specific SSL issue on my Android. If I try to
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue. When I run: try { $as ->setForename($_POST['fname']) ->setSurname($_POST['sname']) ->setEmail($_POST['email']) ->setUser($_POST['user'])
I have an issue with cakephp's login functionality. When I try to login under

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.