Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Static members in static library
QtWS25 Last Chance

Static members in static library

Scheduled Pinned Locked Moved Solved General and Desktop
static librarystatic methodlinker
6 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark81
    wrote on 16 May 2016, 10:07 last edited by
    #1

    I have a QWidget project with several sub-projects as static libraries.
    One of them contains s tatic member (I tried also with a static function).

    
    class myClass : public QObject
    {
        Q_OBJECT
    
    public:
        explicit myClass(QObject *parent = 0);
        static int myStaticMember;
    }
    
    

    The library compiles fine (i.e. libmyClass is generated).
    In another static library of my project I want to use myStaticMember.
    Thus I imported the library in the project file:

    LIBS += -L$$OUT_PWD/../myClass/ -lmyClass
    INCLUDEPATH += $$PWD/../myClass
    DEPENDPATH += $$PWD/../myClass
    PRE_TARGETDEPS += $$OUT_PWD/../myClass/libmyClass.a
    

    Also this library compiles fine, and contains a call to that member:

    #include "myClass.h"
    //
    qDebug() << myClass::myStaticMember;
    

    The problem rises when I build the whole project. I get the following error:

    myClass/myClass.cpp:15: error: undefined reference to `myClass::myStaticMember'
    

    Another weird thing is that the line pointed by the error is few lines below the actual call to the myStaticMember - and it isn't related at all (I declare a QTimer...).

    Why the linker cannot find the member?

    K 1 Reply Last reply 16 May 2016, 10:13
    0
    • M Mark81
      16 May 2016, 10:07

      I have a QWidget project with several sub-projects as static libraries.
      One of them contains s tatic member (I tried also with a static function).

      
      class myClass : public QObject
      {
          Q_OBJECT
      
      public:
          explicit myClass(QObject *parent = 0);
          static int myStaticMember;
      }
      
      

      The library compiles fine (i.e. libmyClass is generated).
      In another static library of my project I want to use myStaticMember.
      Thus I imported the library in the project file:

      LIBS += -L$$OUT_PWD/../myClass/ -lmyClass
      INCLUDEPATH += $$PWD/../myClass
      DEPENDPATH += $$PWD/../myClass
      PRE_TARGETDEPS += $$OUT_PWD/../myClass/libmyClass.a
      

      Also this library compiles fine, and contains a call to that member:

      #include "myClass.h"
      //
      qDebug() << myClass::myStaticMember;
      

      The problem rises when I build the whole project. I get the following error:

      myClass/myClass.cpp:15: error: undefined reference to `myClass::myStaticMember'
      

      Another weird thing is that the line pointed by the error is few lines below the actual call to the myStaticMember - and it isn't related at all (I declare a QTimer...).

      Why the linker cannot find the member?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 16 May 2016, 10:13 last edited by
      #2

      @Mark81
      What platform are you running, windows or *nix? Do you have that static member defined in the cpp file for myClass?

      Read and abide by the Qt Code of Conduct

      M 1 Reply Last reply 16 May 2016, 10:16
      0
      • K kshegunov
        16 May 2016, 10:13

        @Mark81
        What platform are you running, windows or *nix? Do you have that static member defined in the cpp file for myClass?

        M Offline
        M Offline
        Mark81
        wrote on 16 May 2016, 10:16 last edited by
        #3

        @kshegunov Linux. mmm, no, it's just a public variable. What do you mean with "define in the cpp file"?

        K 1 Reply Last reply 16 May 2016, 10:20
        0
        • M Mark81
          16 May 2016, 10:16

          @kshegunov Linux. mmm, no, it's just a public variable. What do you mean with "define in the cpp file"?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 16 May 2016, 10:20 last edited by kshegunov
          #4

          @Mark81 said:
          Having this:

          class myClass
          {
               static int myStaticMember;
          }
          

          You should also do in the cpp file (usually on top):

          int myClass::myStaticMember = 12345; //< Definition and initialization
          

          On Linux the archive (static lib) is not checked for pretty much anything before linking, so you can have the *.a generated and still fail to link.

          Read and abide by the Qt Code of Conduct

          M 1 Reply Last reply 16 May 2016, 10:23
          1
          • K kshegunov
            16 May 2016, 10:20

            @Mark81 said:
            Having this:

            class myClass
            {
                 static int myStaticMember;
            }
            

            You should also do in the cpp file (usually on top):

            int myClass::myStaticMember = 12345; //< Definition and initialization
            

            On Linux the archive (static lib) is not checked for pretty much anything before linking, so you can have the *.a generated and still fail to link.

            M Offline
            M Offline
            Mark81
            wrote on 16 May 2016, 10:23 last edited by
            #5

            @kshegunov Ok, I thought is was optional, not mandatory. My fault. Now it compiles fine, thanks.

            K 1 Reply Last reply 16 May 2016, 10:26
            0
            • M Mark81
              16 May 2016, 10:23

              @kshegunov Ok, I thought is was optional, not mandatory. My fault. Now it compiles fine, thanks.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 16 May 2016, 10:26 last edited by
              #6

              @Mark81
              No problem.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              4/6

              16 May 2016, 10:20

              • Login

              • Login or register to search.
              4 out of 6
              • First post
                4/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved