Solved

Add DLL Functions to a c++ program for linux

Posted on 2003-11-17
10
321 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

13 Experts available now in Live!

Get 1:1 Help Now