Solved

Add DLL Functions to a c++ program for linux

Posted on 2003-11-17
10
322 Views
Last Modified: 2010-04-02
Is it possible to load a DLL library of functions to a c++ program written for linux?
0
Comment
Question by:slogalbo
  • 6
  • 4
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9766167
Sure it is - see the HOWTO at http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html

#include <iostream>
#include <dlfcn.h>


int main() {
    using std::cout;
    using std::cerr;

    cout << "C++ dlopen demo\n\n";

    // open the library
    cout << "Opening hello.so...\n";
    void* handle = dlopen("./hello.so", RTLD_LAZY);
   
    if (!handle) {
        cerr << "Cannot open library: " << dlerror() << '\n';
        return 1;
    }
   
    // load the symbol
    cout << "Loading symbol hello...\n";
    typedef void (*hello_t)();
    hello_t hello = (hello_t) dlsym(handle, "hello");
    if (!hello) {
        cerr << "Cannot load symbol 'hello': " << dlerror() <<
            '\n';
        dlclose(handle);
        return 1;
    }
   
    // use it to do the calculation
    cout << "Calling hello...\n";
    hello();
   
    // close the library
    cout << "Closing library...\n";
    dlclose(handle);
}

0
 

Author Comment

by:slogalbo
ID: 9766244
Thanks so much for the quick response.

Any Idea where I can find the API for download?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9766270
Which 'API' are you looking for? 'dlopen()', 'dlsym()' and 'dlclose()' support comes with every Linux installation, just type

man dlopen

:o)
0
 
LVL 86

Expert Comment

by:jkr
ID: 9766283
0
 

Author Comment

by:slogalbo
ID: 9766530
Thanks, this helps me more than you know.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 9773335
Hm, so, do you have additional questions or can we close this one?
0
 

Author Comment

by:slogalbo
ID: 9773361
Well I would still like to know if I can download this API for use in a Win32 environment, or if it is specifically for linux.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9773423
The above is specifically for Linux. Win32 has it's own mechanisms to do that, see e.g. http://www.codeguru.com/win32/dyndllclass.shtml ("Dynamically loading classes from DLLs "). However, you could create an abstraction, as the similarities overbalance, e.g.

#ifdef WIN32
#define dlopen(x,y) LoadLibrary(x)
#define dlsym(x,y) GetProcAddress(x,y)
#define dlclose(x) FreeLibrary(x)
#endif

0
 

Author Comment

by:slogalbo
ID: 9773462
Okay this has helped me.  The thread is closed.

Thanks again.
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9773682
>>The thread is closed

Hm, to close this, you'll need to click on 'accept comment as answer' next to one of my comments :o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now