ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have made a programme that uses a shared lib as a class factory. The problem is that the classes from the factory use a class named Element. So as the main program. When I compile and link, I have to include the element class in both main and library. Otherwise, it doesn't link.
Is there a way to include the ELment class only once ?
Are you talking about the class definition, or the definitions of its functions? Are you talking about inadvertently #include-ing the same file twice?
ta0kira
Thanks for the answer. I am talking about the cpp file. Iunderstand that the header file is mandatory, especially if the classes are used. But I would think that the cpp file defining the functions should be included only once (in the main program or in the library) and used during the link.
Assuming your "class factory" is in a Linux shared library (.so), and further assuming your code doesn't explicitly instantiate any instances of any of the derived types your "factory" produces, I think you've basically got two alternatives:
a) Declare dummy static variables (just to force the linker to bring in the code)
... or ...
b) Manually invoke dlopen(), dlsym() and friends:
Assuming your "class factory" is in a Linux shared library (.so)
That's the case.
Quote:
Originally Posted by paulsm4
assuming your code doesn't explicitly instantiate any instances of any of the derived types your "factory" produces
Yes but the problem is not with the derived types the factory produces, it is with some classes used by the derived classes AND the main program.
Quote:
Originally Posted by paulsm4
b) Manually invoke dlopen(), dlsym()
THat's what I use ;-)
I'm not sure I was clear enough so here is some code showing where the problem lies :
Main program :
Code:
#include "Element.hpp"
Element e1;
Derived *p;
Derived *(*pInvoke)();
/* I use the Element class in main program */
e1.diplay();
/* I use dlopen/dlsym to get a pointer to the factory */
p = pInvkoke();
/* The derived class also uses and instanciates the derived class */
p.display_element();
Derived class :
Code:
class c1 : public Derived
{
/* Some functional private code */
void display_element()
{
Element e;
e.display_element();
}
}
In that example, the problem is that I have to include the Element.cpp defining the Element class in both main and shared library. From my point of view, it should be enough to have the Element.cpp file in the main program, not in the shared lib. The shared lib should use only the Element.hpp and it should be linked to the functions declared in Element.cpp when linking (when it is loaded).
a) Declare dummy static variables (just to force the linker to bring in the code)
This should only be necessary to instantiate templates, though you might have a problem with in-context function definitions being optimized out as unused inlines. I think the problem here might be a .cpp with both the class declaration and the function definitions in it.
ta0kira
For information, I was able to solve this problem by using the flag -rdynamic when linking the main executable. It allows to export the symbols from the main executable to the dynamically loaded library. Then, there is no more link problem when the shared library is loaded, even if it has been built without the Element.cpp file.
I think I understand your problem now. Normally it's better to solve the problem without additional build options, so normally one would link the base class into a shared library then link both the main program and the library with the derived class to the base class' library:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.