Declaring a class as extern
Hi, I am currently having a problem regarding an extern class variable declaration and definition.
I have a qt subdirs project composed of several projects. The other projects have a config as dynamiclib as they are to be loaded as dlls in the application.
I am implementing a facade class that loads the dlls dynamically and I assign them to qwidgets. To use the instances, I just created a function that returns the desired widget.
Ive declared it this way.
Facade.h
class CFacade
{
public:
QWidget* get_instance_ptr(int i);
private:
QWidget *widget1;
QWidget * widget2;
};
extern CFacade FacadeObj;
Facade.cpp
I defined the extern variable here..
CFacade FacadeObj;
In my subdirs project, with a general form:
subdirs1.pro\
subdirs2.pro\
subdirsmain.pro
subdirs1 has the facade class, since in the build order, it should be built first in order for the other projects
to access the same extern facade class variable.
I want to use the extern facade var in subdirsmain so I just declare the header in one of my cpps that use it but it gets an error that the extern facade class var is an undefined reference. could anyone give some inputs as to what causes this?
Re: Declaring a class as extern
It's not sufficient that you create an extern variable. You need to let the compiler know that your class' members are inside another shared library.
How to create a shared library.
Re: Declaring a class as extern
Specify Lib and path (Specify "Subdirs1.pro" output lib name file path in "subdirsmain.pro" file).
eg: (in subdirsmain.pro)
LIBS += -L../subdirs1/debug/subdir1lib
Re: Declaring a class as extern
I would suggest going with a singleton approach rather than using an extern global variable.
Re: Declaring a class as extern
hi thanks for the replies. what would be the edge of the singleton approach over an extern global variable?
Re: Declaring a class as extern
Quote:
Originally Posted by
ehnuh
hi thanks for the replies. what would be the edge of the singleton approach over an extern global variable?
Cleaner API, less error prone access and total control over when the variable is created and destroyed (a global variable is created before main() is executed and destroyed after main() returns). It also assures there is just one instance of the class in the whole program.
Re: Declaring a class as extern
thanks for the info! I have tried declaring and defining the extern class variable in one pro file. Since I have multiple pro files, I want to access the CFacade FacadeObj I declared and defined. I just add the header of the FacadeObj to the cpp files found in the other pro files (I also added a header for a global shared library since it will be declared in another pro file)...I am not sure what I am still missing but when I build the whole subdirs proj, the
FacadeObj I used in the pro file is undefined. The error is "undefined reference to Facadobj"