Solved

Add DLL Functions to a c++ program for linux

Posted on 2003-11-17
10
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 9766283
0
 

Author Comment

by:slogalbo
ID: 9766530
Thanks, this helps me more than you know.
0
 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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