Results 1 to 7 of 7

Thread: Declaring a class as extern

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  2. #2
    Join Date
    Jun 2010
    Location
    Pretoria, South Africa
    Posts
    22
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. The following user says thank you to Robbie for this useful post:

    ehnuh (8th November 2012)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    ehnuh (8th November 2012)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Declaring a class as extern

    I would suggest going with a singleton approach rather than using an extern global variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    ehnuh (8th November 2012)

  8. #5
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Declaring a class as extern

    Quote Originally Posted by ehnuh View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    ehnuh (8th November 2012)

  11. #7
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default 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"

Similar Threads

  1. Problems compiling when using an extern
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2011, 12:32
  2. Extern Variable linkage error
    By mebingj in forum Newbie
    Replies: 3
    Last Post: 26th January 2011, 12:06
  3. extern QWidgetList *qt_modal_stack
    By Alex_123 in forum Qt Programming
    Replies: 5
    Last Post: 20th December 2008, 12:24
  4. extern class
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2007, 21:08
  5. problem declaring a class
    By mickey in forum General Programming
    Replies: 1
    Last Post: 26th May 2006, 18:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.