Is it possible to get location of dependent libraries from within code












1















I want to load some dependent libraries from my program with dlopen function. Is it possible to know actual location of these libraries?



eg, ldd shows all dependent libraries with paths in system. How does it work? Is it possible to get paths to correspondent libraries I need to load with dlopen via some call from my C++ code?










share|improve this question























  • Is it an "installed" library you're trying to load?

    – YSC
    Jan 3 at 10:26











  • ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

    – Matthieu Brucher
    Jan 3 at 10:28











  • yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

    – dojo_user
    Jan 3 at 10:28
















1















I want to load some dependent libraries from my program with dlopen function. Is it possible to know actual location of these libraries?



eg, ldd shows all dependent libraries with paths in system. How does it work? Is it possible to get paths to correspondent libraries I need to load with dlopen via some call from my C++ code?










share|improve this question























  • Is it an "installed" library you're trying to load?

    – YSC
    Jan 3 at 10:26











  • ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

    – Matthieu Brucher
    Jan 3 at 10:28











  • yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

    – dojo_user
    Jan 3 at 10:28














1












1








1








I want to load some dependent libraries from my program with dlopen function. Is it possible to know actual location of these libraries?



eg, ldd shows all dependent libraries with paths in system. How does it work? Is it possible to get paths to correspondent libraries I need to load with dlopen via some call from my C++ code?










share|improve this question














I want to load some dependent libraries from my program with dlopen function. Is it possible to know actual location of these libraries?



eg, ldd shows all dependent libraries with paths in system. How does it work? Is it possible to get paths to correspondent libraries I need to load with dlopen via some call from my C++ code?







c++ linux shared-libraries executable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 10:25









dojo_userdojo_user

135




135













  • Is it an "installed" library you're trying to load?

    – YSC
    Jan 3 at 10:26











  • ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

    – Matthieu Brucher
    Jan 3 at 10:28











  • yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

    – dojo_user
    Jan 3 at 10:28



















  • Is it an "installed" library you're trying to load?

    – YSC
    Jan 3 at 10:26











  • ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

    – Matthieu Brucher
    Jan 3 at 10:28











  • yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

    – dojo_user
    Jan 3 at 10:28

















Is it an "installed" library you're trying to load?

– YSC
Jan 3 at 10:26





Is it an "installed" library you're trying to load?

– YSC
Jan 3 at 10:26













ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

– Matthieu Brucher
Jan 3 at 10:28





ldd uses LD_LIBRARY_PATH to get the path to the required libraries, for dlopen, it can be anywhere on the filesystem.

– Matthieu Brucher
Jan 3 at 10:28













yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

– dojo_user
Jan 3 at 10:28





yup it is. For example it could be some sort of runtime libs(eg nvidia cuda) that I need at certain time not always.

– dojo_user
Jan 3 at 10:28












1 Answer
1






active

oldest

votes


















1














From man dlopen one can read:




The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):




  • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.


  • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)


  • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.


  • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.


  • The directories /lib and /usr/lib are searched (in that order).





So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020383%2fis-it-possible-to-get-location-of-dependent-libraries-from-within-code%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    From man dlopen one can read:




    The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):




    • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.


    • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)


    • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.


    • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.


    • The directories /lib and /usr/lib are searched (in that order).





    So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.






    share|improve this answer




























      1














      From man dlopen one can read:




      The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):




      • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.


      • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)


      • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.


      • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.


      • The directories /lib and /usr/lib are searched (in that order).





      So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.






      share|improve this answer


























        1












        1








        1







        From man dlopen one can read:




        The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):




        • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.


        • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)


        • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.


        • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.


        • The directories /lib and /usr/lib are searched (in that order).





        So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.






        share|improve this answer













        From man dlopen one can read:




        The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details):




        • (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched.


        • If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)


        • (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched.


        • The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.


        • The directories /lib and /usr/lib are searched (in that order).





        So, if the desired library is "installed", a simple dlopen("foobar.so", flag) will do.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 10:30









        YSCYSC

        25.3k557112




        25.3k557112
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020383%2fis-it-possible-to-get-location-of-dependent-libraries-from-within-code%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas