Re: Query regarding dynamic library
Re: Query regarding dynamic library
- Subject: Re: Query regarding dynamic library
- From: "parag vibhute" <email@hidden>
- Date: Mon, 10 Dec 2007 20:57:31 +0530
Thanks for reply.
From ur writeup, I understood that in order to load library at runtime, dynamic loader needs to get address of function. With dlopen, we can only get address of C function. Now in order to get address of C++ class member function at runtime, only way is to access its address through virtual table. For that, we require to declare C++ class member public function as virtual. And you are right, it is only required when we try to use dynamic library at run time. If we load dynamic library at launch time of application, then linker keeps reference to non-virtual C++ class member function in executable bcoz of which we successfully run of application which is dependant on dynamic library. I hope I am right, am I?
Thanks again,
palav
On Dec 10, 2007 8:19 PM, Alastair Houghton <
email@hidden> wrote:
On 10 Dec 2007, at 09:32, parag vibhute wrote:
> Dynamic library mentions following statement (Page 28):
>
> Dynamic libraries that make a class available to its clients must
> include the virtual keyword in the declaration of all the class's
> methods, except for its constructors and destructors. For example,
> following listing shows the declaration for the Person class.
>
> class Person {
> private:
> char _person_name[30];
> public:
> Person();
> virtual void set_name(char person_name[]);
> virtual char* name();
> };
>
>
> Why is it necessary to include virtual keyword here?
(I assume this is talking about using dlopen() to load the code.)
The reason is that if you do
class Foo {
public:
void bar();
};
then you call it from code like so
void my_function(Foo *foo) {
foo->bar();
}
the compiler does something like this:
Foo::bar(foo);
which uses the linker to look up the symbol "Foo::bar" and substitutes
it into the code at build time.
The only way to get around this is to use weak linking, and it will
only work with dlopen()-loaded libraries if you also disable the two-
level namespace feature (I think) and pass RTLD_GLOBAL when loading
them. Doing that is pretty unpleasant.
If, on the other hand, you do
class Foo {
public:
virtual void bar();
};
and then you call it from code, e.g.
void my_function(Foo *foo) {
foo->bar();
}
the compiler actually does something like this:
(*foo->vtable->bar)(foo);
i.e. it obtains the address of the method to call *from the object*,
at run-time, rather than by using the linker at build time. It isn't
using a symbol at all to do it, so no linking is necessary.
Hopefully that makes sense.
Kind regards,
Alastair.
--
http://alastairs-place.net
--
There are many things in your life that will catch your eye but only a few will catch your heart....pursue those'.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden