Solved

Add DLL Functions to a c++ program for linux

Posted on 2003-11-17
10
325 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.
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.

726 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