public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to create objects of a class defined in .so file?
@ 2000-12-04  6:35 Rajesh Lolam
  2000-12-05  9:00 ` Alexandre Oliva
  0 siblings, 1 reply; 2+ messages in thread
From: Rajesh Lolam @ 2000-12-04  6:35 UTC (permalink / raw)
  To: gcc-help

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/



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-12-05  9:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-04  6:35 How to create objects of a class defined in .so file? Rajesh Lolam
2000-12-05  9:00 ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).