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?

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 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.
  • StokestackS Offline
    StokestackS Offline
    Stokestack
    wrote on last edited by
    #1

    I'm building a library (trying to get Qt to build the Google protocol buffer runtime).

    First I ran the autoconfig scripts that Google provides, which generated a config.h file that #defines a bunch of flags for the environment.

    I've added that config.h file to my Qt project along with all the necessary source, and also added a line to project's _global.h file to include config.h.

    However, when I build, the #defines in config.h are not seen. It's probably a pretty basic problem, but I don't know what it is. Anybody?

    Thanks!

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on 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.

      StokestackS 1 Reply Last reply
      5
      • Kent-DorfmanK Kent-Dorfman

        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.

        StokestackS Offline
        StokestackS Offline
        Stokestack
        wrote on 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
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          StokestackS 1 Reply Last reply
          4
          • Christian EhrlicherC Christian Ehrlicher

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

            StokestackS Offline
            StokestackS Offline
            Stokestack
            wrote on 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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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

              StokestackS 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                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.

                StokestackS Offline
                StokestackS Offline
                Stokestack
                wrote on 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
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    mrjjM 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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
                      • StokestackS Offline
                        StokestackS Offline
                        Stokestack
                        wrote on 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.

                        Pablo J. RoginaP 1 Reply Last reply
                        0
                        • StokestackS Stokestack

                          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.

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on 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

                          • Login

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