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
Forum Updated to NodeBB v4.3 + New Features

Regarding to the main.cpp file using global vars

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 909 Views 1 Watching
  • 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 25 Apr 2019, 14:55 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!.

    J A 2 Replies Last reply 25 Apr 2019, 15:01
    0
    • U U7Development
      25 Apr 2019, 14:55

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

      J Offline
      J Offline
      JonB
      wrote on 25 Apr 2019, 15:01 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 25 Apr 2019, 15:16 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
          25 Apr 2019, 14:55

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 25 Apr 2019, 17:22 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.

          J 1 Reply Last reply 25 Apr 2019, 18:50
          4
          • U Offline
            U Offline
            U7Development
            wrote on 25 Apr 2019, 17:45 last edited by
            #5

            All clear!.. thanks so much all..

            1 Reply Last reply
            0
            • A aha_1980
              25 Apr 2019, 17:22

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

              J Offline
              J Offline
              JonB
              wrote on 25 Apr 2019, 18:50 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?

              A 1 Reply Last reply 25 Apr 2019, 18:54
              0
              • J JonB
                25 Apr 2019, 18:50

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

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 25 Apr 2019, 18:54 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.

                J 1 Reply Last reply 25 Apr 2019, 19:03
                2
                • A aha_1980
                  25 Apr 2019, 18:54

                  @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)

                  J Offline
                  J Offline
                  JonB
                  wrote on 25 Apr 2019, 19:03 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.

                  A 1 Reply Last reply 25 Apr 2019, 19:05
                  0
                  • J JonB
                    25 Apr 2019, 19:03

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

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 25 Apr 2019, 19:05 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

                    2/9

                    25 Apr 2019, 15:01

                    topic:navigator.unread, 7
                    • Login

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