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

  • Home
  • SEARCH
  • 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 441259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:57:16+00:00 2026-05-12T20:57:16+00:00

How can I get the path of the shared library from within the library

  • 0

How can I get the path of the shared library from within the library itself?

In other words, let’s say that library X is loaded using dlopen(), how can I get access to the path that was used to load the said library from within the library itself?

Note that I cannot have the agent that loaded the library in the first place hand me this parameter.

UPDATED: Here is way that works with static variables:

std::string wdir;

namespace {
    class dynamic_library_load_unload_handler {
         public:
              dynamic_library_load_unload_handler(){
                    Dl_info dl_info;
                    dladdr((void *) NP_Initialize, &dl_info);

                    std::string path(dl_info.dli_fname);
                    wdir = path.substr( 0, path.find_last_of( '/' ) +1 );
              }
              ~dynamic_library_load_unload_handler(){
                    // Code to execute when the library is unloaded
              }
    } dynamic_library_load_unload_handler_hook;
}
  • 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-05-12T20:57:16+00:00Added an answer on May 12, 2026 at 8:57 pm

    The dynamic linker actually searches several places to find each dynamic library. These include (from man ld.so):

    • Paths given by the environment variable LD_LIBRARY_PATH
    • Paths baked into the binary load the library under the DT_RUNPATH entry
    • The cache file /etc/ld.so.cache
    • /lib and /usr/lib

    If you want to get the path for a specific shared library, I would recommend the dladdr function. From the man page:

    The function dladdr() takes a function pointer and tries to resolve
    name and file where it is located. Information is stored in the
    Dl_info structure:

    typedef struct {
        const char *dli_fname;  /* Pathname of shared object that
                                   contains address */
        void       *dli_fbase;  /* Address at which shared object
                                   is loaded */
        const char *dli_sname;  /* Name of nearest symbol with address
                                   lower than addr */
        void       *dli_saddr;  /* Exact address of symbol named
                                   in dli_sname */
    } Dl_info;
    

    If no symbol matching addr could be found, then dli_sname and
    dli_saddr are set to NULL.

    dladdr() returns 0 on error, and non-zero on success.

    So you just give it a function pointer, and it will give you the name of the file which supplies it and a bunch of other information. So for instance, you could have a constructor in a library call this on itself to find out the full path of the library:

    #define _GNU_SOURCE
    #include <dlfcn.h>
    #include <stdio.h>
    
    __attribute__((constructor))
    void on_load(void) {
        Dl_info dl_info;
        dladdr((void *)on_load, &dl_info);
        fprintf(stderr, "module %s loaded\n", dl_info.dli_fname);
    }
    

    This function also works on OS X with the same semantics.

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

Sidebar

Ask A Question

Stats

  • Questions 235k
  • Answers 235k
  • 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 I found the problem: It's because I had explicitly bound… May 13, 2026 at 6:13 am
  • Editorial Team
    Editorial Team added an answer Why do you need to do that? Consider setting the… May 13, 2026 at 6:13 am
  • Editorial Team
    Editorial Team added an answer You can do something like this: private void OpenConfirmPrompt() {… May 13, 2026 at 6:13 am

Related Questions

I have a Shared Library wise.so. How I can use it in my programm?
In a team environment on a Linux system I just pulled the latest code
I am trying to use the axis-java2wsdl ant task to create a wsdl from
I am trying to use Zend's Gbase library, but I cannot figure out how

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.