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

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

    kshegunovK 1 Reply Last reply
    0
    • M Mark81

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      0
      • kshegunovK kshegunov

        @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 last edited by
        #3

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

        kshegunovK 1 Reply Last reply
        0
        • M Mark81

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          1
          • kshegunovK kshegunov

            @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 last edited by
            #5

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

            kshegunovK 1 Reply Last reply
            0
            • M Mark81

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Mark81
              No problem.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

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