Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: [C++] Load dll or so

  1. #1
    Join Date
    Oct 2008
    Location
    /home/brad
    Beans
    591
    Distro
    Ubuntu 11.04 Natty Narwhal

    [C++] Load dll or so

    Is there a definite way to load an so file (preferably also dll)? Code I found online is extremely difficult to understand and example code doesn't compile. Thanks!
    Windows free as of 11/18/2008.
    Feel free to message me on Skype if you have questions on anything. I'm happy to help!
    Note: If things don't work, try this: Reboot. Check all wires. Check Again.

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: [C++] Load dll or so

    You will need to be more specific about your problem. Typically an application will load the shared-object library when needed, presuming of course it was linked to use it.

    Some issues arise when the application cannot find the shared-object library because the path is either not defined in /etc/ld.so.conf (or other related area) or not defined in LD_LIBRARY_PATH.

    I can help you, but as I stated earlier, you will need to be more specific with your problem.

  3. #3
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: [C++] Load dll or so

    If you want to dynamically load a library, you should take a look at the dl* functions in <dlfcn.h>, specifically:

    Code:
    /* load a dynamic library into memory */
    void *dlopen(const char *filename, int flag);
    /* return the address of the specified symbol */
    void *dlsym(void *handle, const char *symbol);
    This is of course, for Linux. Windows has a separate but similar set of functions -- LoadLibrary() and GetProcAddress().
    Here we are, trapped in the amber of the moment. There is no why.

  4. #4
    Join Date
    Oct 2008
    Location
    /home/brad
    Beans
    591
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [C++] Load dll or so

    as far as i know, the functions should be:
    PHP Code:
    int main(){
    void *handle dlopen("libexample.so",RD_LAZY);
    void *function = dlsym(*handleexample_function);
    //then do i just do
    function(); 
    Right? Only, it doesn't work. An example code for a .so would be
    PHP Code:
    int main(){
    printf("hello world!");
    return 
    0;

    Thanks for your help!

    EDIT:
    so here is the code specifically that I am trying to compile.
    library.cpp:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
            cout << "hello world!";
            return 0;
    }
    and the program to execute it:
    execute.cpp
    Code:
    #include <dlfcn.h>
    using namespace std;
    int main()
    {
            void *handle = *dlopen("libexample.so",RD_LAZY);
            void *function = *dlsym(*handle,"main");
            *function();
            return 0;
    }
    I can compile the library fine with: "gcc -shared -o libexample.so library.cpp" and with a -ldl flag as well, but compiling the program with "gcc -o execute execute.cpp" returns this error:
    Code:
    execute.cpp: In function ‘int main()’:
    execute.cpp:5: error: ‘RD_LAZY’ was not declared in this scope
    execute.cpp:6: error: ‘void*’ is not a pointer-to-object type
    execute.cpp:7: error: ‘function’ cannot be used as a function
    Last edited by djbushido; March 31st, 2009 at 09:12 PM. Reason: tags wrong
    Windows free as of 11/18/2008.
    Feel free to message me on Skype if you have questions on anything. I'm happy to help!
    Note: If things don't work, try this: Reboot. Check all wires. Check Again.

  5. #5
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] Load dll or so

    Well, you should compile with the -ldl flag to link to the dynamic loader's library...

    And also, a library should never have a main() method... Look here to learn how to build libraries: http://crasseux.com/books/ctutorial/...a-library.html

  6. #6
    Join Date
    Oct 2008
    Location
    /home/brad
    Beans
    591
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [C++] Load dll or so

    This looks like it should work. Will try and post back results. BTW - you're located at the origin???
    Windows free as of 11/18/2008.
    Feel free to message me on Skype if you have questions on anything. I'm happy to help!
    Note: If things don't work, try this: Reboot. Check all wires. Check Again.

  7. #7
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: [C++] Load dll or so

    Almost got it. You don't need to mess with dereferencing the void pointer handles -- just pass the pointer to the dl() functions.

    Also, you can use a typedef to tell c++ what the function you are looking for is supposed to accept/return:
    Code:
    /* suppose the function we are looking for is:
     *  int addints(int a, int b)
     * declare a type 'addints_ptr' that is a pointer
     * to a function matching this signature.
     */
    typedef int(*addints_ptr)(int,int);
    
    int main(int argc, char* argv[])
    {
      /* open the library */
      void* handle = dlopen("foo.so", RTLD_LAZY);
      
      if (!handle) {
         cout << "failed to open library" << endl;
         return 0;
      }
      /* resolve the symbol 'addints' */
      addints_ptr addints = (addints_ptr)dlsym(handle, "addints")
    
      if (!addints) {
         cout << "failed to resolve symbol" << endl;
      } else {
         cout << "3+4=" << addints(3, 4) << endl;
      }
      /* clean up */
      dlclose(handle);
    }
    Here we are, trapped in the amber of the moment. There is no why.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: [C++] Load dll or so

    Why would anyone go through the trouble of loading a DLL programatically? I've never seen this done in any of the projects I have supported; but I suppose there must be some rationale to it. Can someone explain?

  9. #9
    Join Date
    Apr 2006
    Location
    Hamilton, New Zealand
    Beans
    198
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++] Load dll or so

    A friend of mine uses this technique when he write game engines to modularize his code, once he has designed the interface it allows him to swap and test different rendering engines without having to recompile the whole app, or for his windows builds it allows him to swap between the directX version and the openGL version easily.

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: [C++] Load dll or so

    Quote Originally Posted by cabalas View Post
    A friend of mine uses this technique when he write game engines to modularize his code, once he has designed the interface it allows him to swap and test different rendering engines without having to recompile the whole app, or for his windows builds it allows him to swap between the directX version and the openGL version easily.
    Great, thanks for the explanation.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •