How to create objects of a class defined in .so file?
Rajesh Lolam
rajeshlolam@rediffmail.com
Mon Dec 4 06:35:00 GMT 2000
Hi all,
Can anyone suggest? How to create objects of a class defined in .so file?
The steps followed by me are as follows:
1> Created a class in file SharedClass.cpp
2> Created SharedClass.o using the following command
c++ -fPIC -g -c -Wall SharedClass.cpp
3> Created SharedClass.so using the following command
c++ -shared -Wl,soname,SharedClass.so -o SharedClass.so SharedClass.o
4> I've used the test program Test.cpp to load shared object.
=======================================================
SharedClass.h FILE:
#include <iostream.h>
#include <string>
#include <vector>
class SharedClass
{
public :
SharedClass();
~SharedClass();
int ReadName();
int DisplayName(int p_iPos);
int DisplayAll();
private :
vector<string> m_sNameStore;
};
=======================================================
Test.cpp FILE:
// System Includes
#include <dlfcn.h>
#include <iostream.h>
#include <string>
#include <vector>
#include "SharedClass.h"
int main()
{
cout<<endl<<"********** Start of Shared Object Test **********"<<endl;
string strLibName = "SharedClass.so";
cout<<"Calling Dynamic Library '"<<strLibName<<"'...."<<endl;
void *dlhandle = dlopen(strLibName.c_str(), RTLD_LAZY);
if(!dlhandle)
{
cout<<"Error: Unable to point to shared library '"<<strLibName
<<"'!"<<endl;
cout<<"********** End of Shared Object Test **********"<<endl<<endl;
return (0);
}
cout<<"Called Dynamic Library '"<<strLibName<<"'."<<endl;
cout<<"Dynamic Library Handle Created <"<<&dlhandle<<">."<<endl;
string strFuncName = "SharedClass";
SharedClass (*ptrToFunc)();
ptrToFunc = (SharedClass (*) ())dlsym(dlhandle, strFuncName.c_str());
if(ptrToFunc == NULL)
{
cout<<"Error: Could not point to function '"<<strFuncName
<<"'!"<<endl;
cout<<"********** End of Shared Object Test **********"<<endl<<endl;
dlclose(dlhandle);
return (0);
}
else
{
cout<<"Found Constructor for function '"<<strFuncName<<"'."<<endl;
}
ptrToFunc->DisplayAll();
cout<<".... After Calling DisplayAll() ...."<<endl;
ptrToFunc->ReadName();
cout<<".... After Calling ReadName() ...."<<endl;
delete ptrToFunc;
dlclose(dlhandle);
cout<<"********** End of Shared Object Test **********"<<endl<<endl;
return (0);
}
=======================================================
The issues are:
1> How to create an object of class (SharedClass)?
2> How to use methods of class (SharedClass?)
Thanks & Regards,
Rajesh
_____________________________________________________
Chat with your friends as soon as they come online. Get Rediff Bol at
http://bol.rediff.com
Participate in crazy auctions at http://auctions.rediff.com/auctions/
More information about the Gcc-help
mailing list