Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Cpp - creating and Linking DLL Files to C++ program       Share
2008-11-03 |  RatheeshTR  | Viewed: 1247  |    1

//  creating  and  Linking  DLL  Files  to  C++  program

#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)();

    
//  reset  errors
    dlerror();

    hello_t hello = (hello_t) dlsym(handle, "hello");

    hello_t hello_R = (hello_t) dlsym(handle, "Ch_Hello");

    const 
char *dlsym_error = dlerror();
    
if ( dlsym_error )    {
          cerr << "Cannot load symbol 'hello': " << dlsym_error <<'\n';

          dlclose(handle);

          return 1;

    }

    

    
//  use  it  to  do  the  calculation
    cout << "Calling hello...\n";

    

    hello();   
//  User  Defined  Functions
    hello_R(); 
//  User  Defined  Functions

        
//  close  the  library
    cout << "Closing library...\n";

    dlclose(handle);

}

//------------------------------------------------------------------

//    DLL    File  Which  we  have  to  be  Created

#include <iostream>

extern "C" 
void hello() {
    std::cout << "hello" << '\n';

}


extern "C" 
void Ch_Hello() {
    std::cout << "Hello Ratheesh" << '\n';

}


//------------------------------------------------------------------


//  Make  File  In  Linux  Plat  Form  and  Windows
//  How  to  be  Create  DLL  and  Shared  File  in  Linux

# This file is part of the C++ dlopen mini HOWTO. You can find the complete
# HOWTO and/or updated versions at
# http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/
#
# Copyright 2002-2006 Aaron Isotton <aaron@isotton.com>
# Licensed under the GNU GPL.

example1: main.cpp hello.so

$(CXX) $(CXXFLAGS) -o example1 main.cpp -ldl


hello.so: hello.cpp

$(CXX) $(CXXFLAGS) -shared -o hello.so hello.cpp


clean:

rm -f example1 hello.so


.PHONY: clean



Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: malathi.v



Related topics
Stack Implementation
cpp stack example

General Topics
tips to protect your computer against virus
elemets of a defect report
measures of defect report
test metrics
agile methodology
test entry and exit criteria
severity and priority
race condtions and memory leak

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us