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

* Re: How to create objects of a class defined in .so file?
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2000-12-05  9:00 UTC (permalink / raw)
  To: Rajesh Lolam; +Cc: gcc-help

On Dec  4, 2000, "Rajesh Lolam" <rajeshlolam@rediffmail.com> wrote:

> 1> How to create an object of class (SharedClass)?

Create an extern "C" function that returns pointers to such objects,
and use dlsym() to obtain a pointer to that function.

> 2> How to use methods of class (SharedClass?)

For this to work, you'll either have to wrap all class methods with
extern "C" functions, and use dlsym to get their addresses, or link
the library explicitly into your application, instead of dlopen()ing
it, so that your code can refer explicitly to the methods.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ 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).