Operating System - HP-UX
1753777 Members
7363 Online
108799 Solutions
New Discussion юеВ

Shared library and C++ classes

 
SOLVED
Go to solution
Nithin Jose
Advisor

Shared library and C++ classes

Consider there is some change to a class which is a part of a shared library. Changes may include addition of a new function(virtual or non-virtual),addition of a new data member etc.

Could someone please explain in which all cases re-compilation/re-link of the applications using the shared library is required?

It will be great if someone can point me to any document regarding the same.

Thanks in advance.
5 REPLIES 5
Dennis Handly
Acclaimed Contributor
Solution

Re: Shared library and C++ classes

The C++ ODR rule says you must recompile everything that uses that class. It appears you want to cheat.

Is that class only used in the shlib or all over?

Note: The use of inline functions may make any of these harder to check.

1) If you add a new data member, then all places where you create/copy/assign an object must be recompiled. Places where you access that member obviously must be recompiled.

2) Adding a non-virtual function should be ok.

3) Adding a new virtual function will cause problems if not at the end. If at the end, then the key function must be recompiled.
Nithin Jose
Advisor

Re: Shared library and C++ classes

Hi Dennis,

Thank you for the prompt response.

>>Is that class only used in the shlib or all over?

The class is used all over. Objects of this class are instantiated by the application, and also the member functions are called.

>>Note: The use of inline functions may make any of these harder to check.

Does that mean compiler optimizations could also make this harder to check?

>>Adding a non-virtual function should be ok

If the class has some other virtual functions, then is there a possibility of any problems if a non-virtual function is added?

Also what is difference internally in adding a virtual function at that end and at some other place?

Thanks in advance.
Dennis Handly
Acclaimed Contributor

Re: Shared library and C++ classes

>The class is used all over. Objects of this class are instantiated by the application, and also the member functions are called.

(The word "application" includes ALL load modules used, not just the executable.)

So every place where instantiated needs to be recompiled.

>Does that mean compiler optimizations could also make this harder to check?

You can get inlining without optimizing. So either the inline keyword, inlining due to optimization in the same file or cross module inlining.

>is there a possibility of any problems if a non-virtual function is added?

No, since to access it, you would need to have the code changed anyway.

>what is difference internally in adding a virtual function at that end and at some other place?

Existing code uses a fixed offset to access the function descriptor in the virtual table and not a symbolic name. So if something is added in the middle, virtual calls will call the wrong function.
Nithin Jose
Advisor

Re: Shared library and C++ classes

Thanks a lot for the information.
Nithin Jose
Advisor

Re: Shared library and C++ classes

Dennis provided the required information I was looking for.