This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: accessing virtual method in shared libs.
- From: Anitha Boyapati <anithab at sankhya dot com>
- To: John Love-Jensen <eljay at adobe dot com>
- Cc: MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Fri, 8 Dec 2006 19:20:00 +0530 (IST)
- Subject: Re: accessing virtual method in shared libs.
Hi John,
On Fri, 8 Dec 2006, John Love-Jensen wrote:
> Hi Anitha,
>
> Your example code did not compile for me. It appears incomplete.
>
> I fixed your code up, and it worked just fine on my system.
Here is the code :
------------------------------------------------
#file : base.cc
#include <iostream>
using namespace std;
namespace XYZ {
class base {
public:
base() {}
virtual void test()
{
cout<<"helo from base test()"<<endl;
}
};
};
*********************
#file:child.cc
#include <iostream>
#include "base.cc"
using namespace XYZ;
using namespace std;
class child : public base {
public:
child() {
cout << "helo world!This is child constructor."<<endl;
}
void test()
{
cout<<"hi..this from child" <<endl;
}
};
extern "C" child* get_type(void)
{
return (new child());
}
********
#file :main.cc
#include <iostream>
#include "base.cc"
#include <dlfcn.h>
using namespace XYZ;
using namespace std;
base* load_libchild();
int main()
{
base *ldchild = load_libchild();
ldchild->test();
}
base* load_libchild()
{
void *handle;
try
{
handle = dlopen("libchild.so",RTLD_LAZY);
void *sym = dlsym(handle,"get_type" );
typedef base* (*Type)(void);
Type ct = (Type)sym;
base* t = (base*)ct();
dlclose(handle); //Segmentation fault cause
return t;
}
catch (const char *err) {
cerr << err << endl;
}
}
***************
script : g++ -shared -o libchild.so child.cc
g++ main.cc base.cc -ldl
-----------------------------------------------------------
Please note removal of dlclose(..) doesnt cause any segmentation fault.
This was used before(when I mentioned user defined class).
Please let me know how dlclose(handle) is responsible for
segmentation fault.
>
> It appears that child.cc, main.cc, nor base.cc have any #include statements.
>
> They use cout, but I do not see any using std::cout; and using std::endl;
> statements.
>
> I see that child is derived from base, but base does not exist as specified.
> XYZ::base exists, but that's not what the code uses.
>
> I don't see where load_libmem is defined.
>
> I don't see where base.cc is being compiled.
>
> I don't see where main.cc is being compiled.
>
> Could you provide a short complete compilable code example, including the
> command line you used to compile each of the files.
>
> You are using GCC 3.2.3 on i686. Any particular operating system, or
> doesn't it matter to you? If it doesn't matter, let's use GNU/Hurd.
>
> Thanks,
> --Eljay
>
>
--
Regards,
Anitha B,
Sankhya Technologies Private Limited.