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. How do I make #defines in a header file visible to the whole project?
Forum Updated to NodeBB v4.3 + New Features

How do I make #defines in a header file visible to the whole project?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 2.0k Views 4 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.
  • K Offline
    K Offline
    Kent-Dorfman
    wrote on 22 Apr 2019, 04:37 last edited by
    #2

    As you are already aware, macros are only visible in the compilation unit where they exist. Inclusion of a global header that contains macros will make those macros available anywhere that header in explicitly included. Where you can run into trouble are if conditional macros block inclusion of the global macros...such is the case with guard macros like you'd use to keep a class definition from being multiply defined. Without digging into the specific code it would be hard to further diagnose your problem.

    As a diagnostic step you could add the g++ option to preprocess each source file without actually compiling, and then inspect and trace the preprocessed output.

    I can't really be any more help that that.

    S 1 Reply Last reply 22 Apr 2019, 07:01
    5
    • K Kent-Dorfman
      22 Apr 2019, 04:37

      As you are already aware, macros are only visible in the compilation unit where they exist. Inclusion of a global header that contains macros will make those macros available anywhere that header in explicitly included. Where you can run into trouble are if conditional macros block inclusion of the global macros...such is the case with guard macros like you'd use to keep a class definition from being multiply defined. Without digging into the specific code it would be hard to further diagnose your problem.

      As a diagnostic step you could add the g++ option to preprocess each source file without actually compiling, and then inspect and trace the preprocessed output.

      I can't really be any more help that that.

      S Offline
      S Offline
      Stokestack
      wrote on 22 Apr 2019, 07:01 last edited by Stokestack
      #3

      @Kent-Dorfman

      The config.h file contains, among many other things, this:

      /* Define if you have POSIX threads libraries and header files. */
      #define HAVE_PTHREAD 1
      

      This is not seen in other files, causing compilation to fail as a result of the following:

      #ifdef _WIN32
      #define WIN32_LEAN_AND_MEAN  // We only need minimal includes
      #include <windows.h>
      #define snprintf _snprintf    // see comment in strutil.cc
      #elif defined(HAVE_PTHREAD)
      #include <pthread.h>
      #else
      #error "No suitable threading library available."
      

      Somehow this config.h file works, in some environment. The question is why it's not working in this one.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 22 Apr 2019, 07:05 last edited by
        #4

        @Stokestack said in How do I make #defines in a header file visible to the whole project?:

        The question is why it's not working in this one.

        Because you did not include config.h here?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply 22 Apr 2019, 07:08
        4
        • C Christian Ehrlicher
          22 Apr 2019, 07:05

          @Stokestack said in How do I make #defines in a header file visible to the whole project?:

          The question is why it's not working in this one.

          Because you did not include config.h here?

          S Offline
          S Offline
          Stokestack
          wrote on 22 Apr 2019, 07:08 last edited by Stokestack
          #5

          @Christian-Ehrlicher

          It's in [project]_global.h.

          Modifying every header file in the entire library to inject an include statement for this header file can't be correct. The configuration scripts don't do that, so there must be a way to compile it without editing every source file.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 22 Apr 2019, 07:15 last edited by Christian Ehrlicher
            #6

            As @Kent-Dorfman already mentioned above you have to include the header file if you want to use it's content.
            Configuration scripts (from whereever) only do what you tell them to do so I don't see what this should have to do with your problem.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply 22 Apr 2019, 07:37
            1
            • C Christian Ehrlicher
              22 Apr 2019, 07:15

              As @Kent-Dorfman already mentioned above you have to include the header file if you want to use it's content.
              Configuration scripts (from whereever) only do what you tell them to do so I don't see what this should have to do with your problem.

              S Offline
              S Offline
              Stokestack
              wrote on 22 Apr 2019, 07:37 last edited by Stokestack
              #7

              @Christian-Ehrlicher

              Well I'm curious as to how this header file is ever effective then. Because there is no script that I can see that goes into every header file in this library and injects an include statement for this config.h file.

              What is the role of the [project]_global.h file if not to declare project-wide definitions and include project-wide headers?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 22 Apr 2019, 07:56 last edited by
                #8

                Hi
                I would guess on they use the features of autoconf+automake to
                make every unit use it.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 22 Apr 2019, 08:46 last edited by
                  #9

                  @mrjj said in How do I make #defines in a header file visible to the whole project?:

                  I would guess on they use the features of autoconf+automake to
                  make every unit use it.

                  Somewhere the generated config.h is included - either directly or indirectly. There is no other way to (except some compiler specific switches but this is more a hack)

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  M 1 Reply Last reply 22 Apr 2019, 08:57
                  2
                  • C Christian Ehrlicher
                    22 Apr 2019, 08:46

                    @mrjj said in How do I make #defines in a header file visible to the whole project?:

                    I would guess on they use the features of autoconf+automake to
                    make every unit use it.

                    Somewhere the generated config.h is included - either directly or indirectly. There is no other way to (except some compiler specific switches but this is more a hack)

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 22 Apr 2019, 08:57 last edited by
                    #10

                    @Christian-Ehrlicher
                    ok, i have not used autoconf+automake that much so if you say
                    they cant rig a make file to auto include pr unit then config.h must be included as
                    there is no other magic possible :)

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Stokestack
                      wrote on 22 Apr 2019, 19:44 last edited by Stokestack
                      #11

                      Thanks a lot for the replies. I just put it into Google's common header file and called it a day.

                      I have much bigger problems to solve in regard to building protobuf for cross-platform use. Seems like no one else is doing it.

                      P 1 Reply Last reply 22 Apr 2019, 20:06
                      0
                      • S Stokestack
                        22 Apr 2019, 19:44

                        Thanks a lot for the replies. I just put it into Google's common header file and called it a day.

                        I have much bigger problems to solve in regard to building protobuf for cross-platform use. Seems like no one else is doing it.

                        P Offline
                        P Offline
                        Pablo J. Rogina
                        wrote on 22 Apr 2019, 20:06 last edited by
                        #12

                        @Stokestack said in How do I make #defines in a header file visible to the whole project?:

                        and called it a day.

                        If your issue is solved, please don't forget to mark your post as such. Thanks.

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0

                        11/12

                        22 Apr 2019, 19:44

                        • Login

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