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. Regarding to the main.cpp file using global vars
QtWS25 Last Chance

Regarding to the main.cpp file using global vars

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 889 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by
    #1

    Hello!.

    0_1556204017277_QT_question1.jpg

    In the image, I have declared a simple int x var at line 15, but I have got a warning telling me that :

    <<a previous extern declaration for non-static variable 'x'>>

    My apologies, it is the first time I see this error, previously I only worked using console applications and have set many different types of variables before the main() entry, never seen this before...

    What is happening?

    Thanks so much!.

    JonBJ aha_1980A 2 Replies Last reply
    0
    • U U7Development

      Hello!.

      0_1556204017277_QT_question1.jpg

      In the image, I have declared a simple int x var at line 15, but I have got a warning telling me that :

      <<a previous extern declaration for non-static variable 'x'>>

      My apologies, it is the first time I see this error, previously I only worked using console applications and have set many different types of variables before the main() entry, never seen this before...

      What is happening?

      Thanks so much!.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @U7Development
      For what you want (probably), put static in front of it:

      static int x;

      Nothing about your question has anything to do with whether your program is console/UI/Qt or not, nor whether it's in main.cpp or before main() or anything. Maybe you're using a different compiler/warnings from whatever you're used to.

      BTW, don't know what compiler you're using (always worth stating for a compilation question!), but can't you click on that warning and Google/read up on it? Something somewhere will explain why it's warning and what you can do about it.

      1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        And just so you do not get tempted.
        No Global Qt objects allowed.
        No Qt object may be created before QApplication in main.

        1 Reply Last reply
        8
        • U U7Development

          Hello!.

          0_1556204017277_QT_question1.jpg

          In the image, I have declared a simple int x var at line 15, but I have got a warning telling me that :

          <<a previous extern declaration for non-static variable 'x'>>

          My apologies, it is the first time I see this error, previously I only worked using console applications and have set many different types of variables before the main() entry, never seen this before...

          What is happening?

          Thanks so much!.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @U7Development

          The warning comes from the Clang Code Model, telling you that the variable is visible in your whole program at link time, but cannot be accessed from other source files as there is no extern declaration before.

          As said before, static is most likely what you want here.

          Qt has to stay free or it will die.

          JonBJ 1 Reply Last reply
          4
          • U Offline
            U Offline
            U7Development
            wrote on last edited by
            #5

            All clear!.. thanks so much all..

            1 Reply Last reply
            0
            • aha_1980A aha_1980

              @U7Development

              The warning comes from the Clang Code Model, telling you that the variable is visible in your whole program at link time, but cannot be accessed from other source files as there is no extern declaration before.

              As said before, static is most likely what you want here.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @aha_1980
              Now that this thread is solved:

              cannot be accessed from other source files as there is no extern declaration before.

              Purely, purely OOI: in C another source file, which happens to know the variable name, can do its own extern int x; and then access that variable. Does C++ forbid that, you cannot access the first file's x unless it chooses to extern it in its header file?

              aha_1980A 1 Reply Last reply
              0
              • JonBJ JonB

                @aha_1980
                Now that this thread is solved:

                cannot be accessed from other source files as there is no extern declaration before.

                Purely, purely OOI: in C another source file, which happens to know the variable name, can do its own extern int x; and then access that variable. Does C++ forbid that, you cannot access the first file's x unless it chooses to extern it in its header file?

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB

                Purely, purely OOI: in C another source file, which happens to know the variable name, can do its own extern int x; and then access that variable. Does C++ forbid that, you cannot access the first file's x unless it chooses to extern it in its header file?

                I think not. However, even in C it is better to put extern in a header. Otherwise the declarations might differ - I had that recently and it caused strange bugs (variable size was wrong which led to overlapping variables, IIRC)

                Qt has to stay free or it will die.

                JonBJ 1 Reply Last reply
                2
                • aha_1980A aha_1980

                  @JonB

                  Purely, purely OOI: in C another source file, which happens to know the variable name, can do its own extern int x; and then access that variable. Does C++ forbid that, you cannot access the first file's x unless it chooses to extern it in its header file?

                  I think not. However, even in C it is better to put extern in a header. Otherwise the declarations might differ - I had that recently and it caused strange bugs (variable size was wrong which led to overlapping variables, IIRC)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @aha_1980
                  Oh, I never meant it was a good idea! I was questioning whether you/the error message were implying that it was now compulsory/forbidden, sounds like it's as-was.

                  aha_1980A 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @aha_1980
                    Oh, I never meant it was a good idea! I was questioning whether you/the error message were implying that it was now compulsory/forbidden, sounds like it's as-was.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JonB as said, I'm not sure.

                    I just wanted to give an example of why it might be a bad idea.

                    In case someone doesn't want to repeat my fails...

                    Qt has to stay free or it will die.

                    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