Cpp - creating and Linking DLL Files to C++ program
Share
// 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:
|
Submitted By:
Prof: Software
Tech: C ,Cpp
|