Utility to ensure appropriate headers were included

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

    Utility to ensure appropriate headers were included


    Recently, there was a Linux program distributed as source code, and it
    compiled fine on the majority of systems. However on some systems, it
    failed to compile. On some of these systems, people were getting
    errors for undeclared tokens, while others were getting linking
    errors.

    Anyway, the problem was that one of the source files was missing:

    #include <stdio.h>

    This wasn't a problem on most systems because some of the other header
    files that were included actually included stdio.h.

    Is there any utility out there that will process a source file and
    notify you if you're depending on a declaration that isn't present in
    a header file that's included directly in the source file?
  • jacob navia

    #2
    Re: Utility to ensure appropriate headers were included

    Tomás Ó hÉilidhe wrote:
    Recently, there was a Linux program distributed as source code, and it
    compiled fine on the majority of systems. However on some systems, it
    failed to compile. On some of these systems, people were getting
    errors for undeclared tokens, while others were getting linking
    errors.
    >
    Anyway, the problem was that one of the source files was missing:
    >
    #include <stdio.h>
    >
    This wasn't a problem on most systems because some of the other header
    files that were included actually included stdio.h.
    >
    Is there any utility out there that will process a source file and
    notify you if you're depending on a declaration that isn't present in
    a header file that's included directly in the source file?
    Yes, that utility is called a C compiler.
    Normally, all compilers will warn you about missing declarations.
    The problem is that people think that compiling means just
    "press 'compile' in the IDE" and ignore completely the errors
    or warnings that the compiler prints.



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

    Comment

    • Richard Heathfield

      #3
      Re: Utility to ensure appropriate headers were included

      jacob navia said:
      Tomás Ó hÉilidhe wrote:
      <snip>
      >>
      >Is there any utility out there that will process a source file and
      >notify you if you're depending on a declaration that isn't present in
      >a header file that's included directly in the source file?
      >
      Yes, that utility is called a C compiler.
      You appear to have mis-read the question.
      Normally, all compilers will warn you about missing declarations.
      He's not talking about missing declarations. He's talking about
      declarations that depend on a header that is included indirectly by
      another header.
      The problem is that people think that compiling means just
      "press 'compile' in the IDE" and ignore completely the errors
      or warnings that the compiler prints.
      No, that isn't the problem. It's *a* problem, but it's not *this* problem.

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • =?UTF-8?q?Harald_van_D=C4=B3k?=

        #4
        Re: Utility to ensure appropriate headers were included

        On Sun, 24 Feb 2008 04:02:31 -0800, Tomás Ó hÉilidhe wrote:
        Recently, there was a Linux program distributed as source code, and it
        compiled fine on the majority of systems. However on some systems, it
        failed to compile. On some of these systems, people were getting errors
        for undeclared tokens, while others were getting linking errors.
        >
        Anyway, the problem was that one of the source files was missing:
        >
        #include <stdio.h>
        >
        This wasn't a problem on most systems because some of the other header
        files that were included actually included stdio.h.
        >
        Is there any utility out there that will process a source file and
        notify you if you're depending on a declaration that isn't present in a
        header file that's included directly in the source file?
        What exactly do you mean?

        a.c:
        #include <stdio.h>
        int myputchar(char c) { return putchar(c); }

        b.c:
        #include "b.h"
        int myputchar(char c) { return putchar(c); }

        b.h:
        #include <stdio.h>

        c.c:
        #include <curses.h>
        int myputchar(char c) { return putchar(c); }

        For which files do you want a notification? I'm guessing you want none for
        a, and one for c. I don't know about b. However, what if <stdio.hdoesn 't
        define putchar, but instead includes a non-standard header which does? On
        such a system (mine happens to declare putchar in <stdio.hand define it
        in <bits/stdio.h>, but the problem does exist for several other standard
        library headers) there's really no difference between a and c that any
        tool could tell without special built-in knowledge. So what do you want
        the tool to do?

        Comment

        • Richard Tobin

          #5
          Re: Utility to ensure appropriate headers were included

          In article <5649c69f-79d8-4e6f-a9a8-181b86a0f35c@q3 3g2000hsh.googl egroups.com>,
          Tomás Ó hÉilidhe <toe@lavabit.co mwrote:
          >Recently, there was a Linux program distributed as source code, and it
          >compiled fine on the majority of systems. However on some systems, it
          >failed to compile. On some of these systems, people were getting
          >errors for undeclared tokens, while others were getting linking
          >errors.
          >
          >Anyway, the problem was that one of the source files was missing:
          >
          >#include <stdio.h>
          >
          >This wasn't a problem on most systems because some of the other header
          >files that were included actually included stdio.h.
          Presumably you mean that one of the system headers included stdio.h,
          because if it was one of the program's headers this wouldn't be a
          problem on "some systems".

          I'm not sure to what extent this is permitted for standard C headers.
          Presumably it's allowed, because all the identifiers they declare are
          reserved. Perhaps someone can quote chapter and verse on this.
          >Is there any utility out there that will process a source file and
          >notify you if you're depending on a declaration that isn't present in
          >a header file that's included directly in the source file?
          I don't think that's possible, since it's perfectly legal for, say,
          stdio.h to work by including some other system headers that declares
          printf().

          -- Richard



          --
          :wq

          Comment

          • =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?=

            #6
            Re: Utility to ensure appropriate headers were included

            Presumably you mean that one of the system headers included stdio.h,
            because if it was one of the program's headers this wouldn't be a
            problem on "some systems".

            Not necessarily. The code could have used some other shared headers,
            something like:

            #include <openssl/decrypt.h>

            The newer version of "decrypt.h" might not include <stdio.h>.

            Comment

            • Paul Hsieh

              #7
              Re: Utility to ensure appropriate headers were included

              On Feb 24, 4:02 am, "Tomás Ó hÉilidhe" <t...@lavabit.c omwrote:
              Recently, there was a Linux program distributed as source code, and it
              compiled fine on the majority of systems. However on some systems, it
              failed to compile. On some of these systems, people were getting
              errors for undeclared tokens, while others were getting linking
              errors.
              >
              Anyway, the problem was that one of the source files was missing:
              >
              #include <stdio.h>
              >
              This wasn't a problem on most systems because some of the other header
              files that were included actually included stdio.h.
              Indeed, a good question. One not addressed by the C standard. One
              can see how the claims of portability seem highly exaggerated in light
              of this.
              Is there any utility out there that will process a source file and
              notify you if you're depending on a declaration that isn't present in
              a header file that's included directly in the source file?
              Well ... I am unaware of such a utility.

              But you could build one manually, probably. The main purpose of the
              standard library header files are to declare prototypes and some
              typedefs. So what you could do is copy your compiler's header files
              to a separate directory structure somewhere, manually remove all
              #include's from those header files (easier said than done) then point
              your compiler's STD LIB include directory to that directory and try a
              test compile. You should not expect this to properly link -- in fact
              you should get errors, but at least it should compile.

              The hard part, I suppose, is building a set of include files that
              truly represented the absolute minimum support that the C language.
              Some of the structures defined may have fields that depend on the
              inclusion of other header files, for example. But I suppose this is a
              doable exercise over a few days, maybe, if one started with a
              compiler's header files, and the C standard, and just worked through
              them.

              You didn't think writing portable C code was going to be easy did you?

              --
              Paul Hsieh
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


              Comment

              • Richard Tobin

                #8
                Re: Utility to ensure appropriate headers were included

                In article <db7ea$47c30981 $541dfcd3$28427 @cache4.tilbu1. nb.home.nl>,
                Harald van Dijk <truedfx@gmail. comwrote:
                >They're reserved "for use as identifiers with external linkage" when their
                >standard headers are not included, and they are reserved "for use as a
                >macro name and as an identifier with file scope in the same name space" if
                >they are.
                Incidentally, I find this wording very unclear. "Reserved for use as X"
                naturally means "can only be used as X", but is presumably intended to
                mean "reserved in such a way that the user mustn't re-use them as X".

                -- Richard
                --
                :wq

                Comment

                • =?UTF-8?q?Harald_van_D=C4=B3k?=

                  #9
                  Re: Utility to ensure appropriate headers were included

                  On Mon, 25 Feb 2008 22:15:47 +0000, Richard Tobin wrote:
                  In article <db7ea$47c30981 $541dfcd3$28427 @cache4.tilbu1. nb.home.nl>,
                  Harald van Dijk <truedfx@gmail. comwrote:
                  >
                  >>They're reserved "for use as identifiers with external linkage" when
                  >>their standard headers are not included, and they are reserved "for use
                  >>as a macro name and as an identifier with file scope in the same name
                  >>space" if they are.
                  >
                  Incidentally, I find this wording very unclear. "Reserved for use as X"
                  naturally means "can only be used as X", but is presumably intended to
                  mean "reserved in such a way that the user mustn't re-use them as X".
                  It can be read from the implementation' s viewpoint: the implementation can
                  only use puts in the ways directly specified in the standard (and mustn't
                  interfere with any other ways it might be used in user code).

                  Comment

                  • CBFalconer

                    #10
                    Re: Utility to ensure appropriate headers were included

                    Keith Thompson wrote:
                    CBFalconer <cbfalconer@yah oo.comwrites:
                    >
                    .... snip ...
                    >
                    >Well, I reread the quoted part, and it seems to me that any
                    >compiler should spit out some 'undefined' error messages. You
                    >can't say a missing definition belongs in any particular file.
                    >The fact that functions require prototypes in C99 should help.
                    >Maybe I am thick.
                    >
                    The key word is *directly*. For example, if a program calls
                    printf, it should have "#include <stdio.h>". If another header
                    the program includes, say "foo.h", happens to include <stdio.h>,
                    then the program will compile without error -- until it's
                    recompiled on a system where "foo.h" *doesn't* happen to include
                    <stdio.h>. What Tomás is looking for is a utility that will
                    warn about this kind of problem.
                    Oh, I see. However, that sort of problem is basic to his include
                    philosophy. When he writes the module that includes foo.h, and
                    needs access to printf, he should have included stdio.h himself.
                    stdio.h (and all standard include files) has protections against
                    any problems due to multiple inclusion.

                    However, I suspect that a decent cross-reference utility will do
                    the job for him.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    Working...