Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. clang complaint on missing previous extern

clang complaint on missing previous extern

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
5 Posts 3 Posters 3.7k 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.
  • K Offline
    K Offline
    koahnig
    wrote on 7 Aug 2018, 16:45 last edited by aha_1980 8 Jul 2018, 19:20
    #1

    In my code I have found the warning below for the declaration of a global variable.

    0_1533659682876_aec6451d-c726-4067-a203-26dcfed0aabb-image.png

    I know the drawbacks of global variables and use them scarcely. I have the variable also in another cpp file with an extern, but not here.
    IIRC in early times we could/did not initialize immediately, but that is handy and avoids duplication. The statement above has been introduced just to check if one of the examples found by google from tutorials will be digested without warning by cland model. However, see yourself.

    I do not really see what I should do differently?
    Certainly I can make it part of a class and declare as static,but sounds odd to me.

    For those fellows checking it out.
    I am on windows 10 64 bit
    Creator is released 4.7.0
    Code model is also for 4.7.0

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GarryJ
      wrote on 29 Aug 2018, 09:39 last edited by GarryJ
      #2

      The extern declaration has to be in a header file, which is then included in all cpp files which use the variables. If the extern declaration is in a cpp file, that declaration is only visible within that cpp file (a.k.a., in that translation unit).

      If you only need the variable within the cpp file, declare it in the cpp file as static. If you need it shared between translation units, declare it in a header file as extern, and then include that header and declare the variable in each cpp file (without extern), as you have above.

      In general, though, most of the time the presence of a global variable in C++ is a leftover from C-style programming from the days of yore, and indicates a design flaw that will only cause more issues down the line. If the message handler is required throughout the program, create it in your main() function and pass it to objects that depend on it using dependency injection. Likewise, if you need to monitor some kind of global status, it's much more preferable to do it within a relevant class, or even create a new one (e.g. StatusMonitor).

      K 1 Reply Last reply 29 Aug 2018, 10:04
      4
      • G GarryJ
        29 Aug 2018, 09:39

        The extern declaration has to be in a header file, which is then included in all cpp files which use the variables. If the extern declaration is in a cpp file, that declaration is only visible within that cpp file (a.k.a., in that translation unit).

        If you only need the variable within the cpp file, declare it in the cpp file as static. If you need it shared between translation units, declare it in a header file as extern, and then include that header and declare the variable in each cpp file (without extern), as you have above.

        In general, though, most of the time the presence of a global variable in C++ is a leftover from C-style programming from the days of yore, and indicates a design flaw that will only cause more issues down the line. If the message handler is required throughout the program, create it in your main() function and pass it to objects that depend on it using dependency injection. Likewise, if you need to monitor some kind of global status, it's much more preferable to do it within a relevant class, or even create a new one (e.g. StatusMonitor).

        K Offline
        K Offline
        koahnig
        wrote on 29 Aug 2018, 10:04 last edited by
        #3

        @GarryJ

        Thanks for response.

        You are completely right. Obviously I did not see the forest in front of trees at that time ;)

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 29 Aug 2018, 10:06 last edited by
          #4

          You may consider using Q_GLOBAL_STATIC, too https://doc.qt.io/qt-5/qglobalstatic.html#Q_GLOBAL_STATIC

          And if you are using the message handler for logs, you don't have to have anything global/static - just use qInstallMessageHandler and then qDebug() can be used to operate on your custom message handler.

          (Z(:^

          K 1 Reply Last reply 29 Aug 2018, 11:18
          4
          • S sierdzio
            29 Aug 2018, 10:06

            You may consider using Q_GLOBAL_STATIC, too https://doc.qt.io/qt-5/qglobalstatic.html#Q_GLOBAL_STATIC

            And if you are using the message handler for logs, you don't have to have anything global/static - just use qInstallMessageHandler and then qDebug() can be used to operate on your custom message handler.

            K Offline
            K Offline
            koahnig
            wrote on 29 Aug 2018, 11:18 last edited by
            #5

            @sierdzio

            Thanks for the hint. Looks interesting.

            Vote the answer(s) that helped you to solve your issue(s)

            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