Tool to determine which headers are needed/not_needed?

Discussion in 'C Programming' started by C_guy, Sep 10, 2008.

  1. C_guy

    C_guy Guest

    Does anyone know of a (hopefully free) tool that can traverse a
    project and determine which "#include"s are not needed or needed in
    every .C file? This would be helpful in removing header inclusions
    that are redundant and/or unnecessary.

    Thanks!
    C_guy, Sep 10, 2008
    #1
    1. Advertising

  2. C_guy

    CBFalconer Guest

    C_guy wrote:
    >
    > Does anyone know of a (hopefully free) tool that can traverse a
    > project and determine which "#include"s are not needed or needed
    > in every .C file? This would be helpful in removing header
    > inclusions that are redundant and/or unnecessary.


    Use your editor. Comment out a #include statement. Recompile. If
    no errors appear, that statement can go. If errors appear, remove
    the comment. Repeat.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 11, 2008
    #2
    1. Advertising

  3. On Wed, 10 Sep 2008 08:10:56 -0700 (PDT), C_guy <>
    wrote:

    >Does anyone know of a (hopefully free) tool that can traverse a
    >project and determine which "#include"s are not needed or needed in
    >every .C file? This would be helpful in removing header inclusions
    >that are redundant and/or unnecessary.


    If you start with none, the compiler will tell every time it
    encounters something that would have been in a header. Add one at a
    time till the diagnostics disappear.

    Other than a pretty small time penalty, including an unnecessary
    header or two usually doesn't cause problems.

    --
    Remove del for email
    Barry Schwarz, Sep 11, 2008
    #3
  4. C_guy

    Guest

    On Sep 10, 7:09 pm, CBFalconer <> wrote:
    > C_guy wrote:
    >
    > > Does anyone know of a (hopefully free) tool that can traverse a
    > > project and determine which "#include"s are not needed or needed
    > > in every .C file?  This would be helpful in removing header
    > > inclusions that are redundant and/or unnecessary.

    >
    > Use your editor.  Comment out a #include statement.  Recompile.  If
    > no errors appear, that statement can go.  If errors appear, remove
    > the comment.  Repeat.



    Surely that's too simplistic - consider a header containing:

    #undef FOO
    #define FOO 17

    Assuming a prior definition of FOO (to something other than 17), one
    could easily see the compilation succeed, yet no longer be producing
    the correct program).

    Or a header that contains:

    int a=1;

    And everywhere else (possibly in the same compilation unit, if needed)
    there's:

    int a;

    Many other potential problems exist

    Which is not to say that sort of things is good programming practice
    (and frankly most dependencies like that probably reflect poor
    design), but we've all seen really bizarre stuff in headers, no?
    , Sep 11, 2008
    #4
  5. C_guy

    Flash Gordon Guest

    wrote, On 11/09/08 02:48:
    > On Sep 10, 7:09 pm, CBFalconer <> wrote:
    >> C_guy wrote:
    >>
    >>> Does anyone know of a (hopefully free) tool that can traverse a
    >>> project and determine which "#include"s are not needed or needed
    >>> in every .C file? This would be helpful in removing header
    >>> inclusions that are redundant and/or unnecessary.

    >> Use your editor. Comment out a #include statement. Recompile. If
    >> no errors appear, that statement can go. If errors appear, remove
    >> the comment. Repeat.

    >
    >
    > Surely that's too simplistic - consider a header containing:
    >
    > #undef FOO
    > #define FOO 17
    >
    > Assuming a prior definition of FOO (to something other than 17), one
    > could easily see the compilation succeed, yet no longer be producing
    > the correct program).
    >
    > Or a header that contains:
    >
    > int a=1;
    >
    > And everywhere else (possibly in the same compilation unit, if needed)
    > there's:
    >
    > int a;
    >
    > Many other potential problems exist


    A far more likely potential problem in my opinion is if the header contains:
    double foo(...)

    Then with the default options on a lot of compilers (or with C89
    conformance added to the defaults) the compiler will *not* complain, it
    will just silently compiler the code with what is now undefined
    behaviour which may or may not work. If foo currently returns an int it
    is even worse, because then (if the parameters are not a problem) it
    *will* work until foo() the return type of foo is changed.

    > Which is not to say that sort of things is good programming practice
    > (and frankly most dependencies like that probably reflect poor
    > design), but we've all seen really bizarre stuff in headers, no?


    You don't need bizarre stuff in the header for commenting it out to
    silently cause problems. Simple good practice can do it.

    The OP would be well advised to ensure that the compiler is told to warn
    about functions being called without a prototype in scope, and as much
    else as possible. This will help catch problems where a header is *not*
    included (directly or indirectly) but is needed. How to do this (and if
    it is possible) is compiler dependant and so should be asked on a group
    dedicated to the compiler.
    --
    Flash Gordon
    Flash Gordon, Sep 11, 2008
    #5
  6. C_guy

    Ian Collins Guest

    Jack Klein wrote:
    > On Wed, 10 Sep 2008 08:10:56 -0700 (PDT), C_guy <>
    > wrote in comp.lang.c:
    >
    >> Does anyone know of a (hopefully free) tool that can traverse a
    >> project and determine which "#include"s are not needed or needed in
    >> every .C file? This would be helpful in removing header inclusions
    >> that are redundant and/or unnecessary.

    >
    > I see you've already got a few recommendations for the comment out and
    > look for errors school of thought, which does work but can take an
    > enormous amount of time for a large source code base.
    >
    > The only tool that I know of that does this, and really well, is not
    > free. PC-Lint, http://www.gimpel.com. This feature is really a nice
    > little bonus. Its code checking function alone is worth many times
    > its price to anyone producing professional code.
    >

    I hadn't looked before, but Sun Studio lint (which is free) also reports
    unnecessary headers.

    --
    Ian Collins.
    Ian Collins, Sep 11, 2008
    #6
  7. C_guy

    Amandil Guest

    On Sep 10, 8:09 pm, CBFalconer <> wrote:
    > C_guy wrote:
    >
    > > Does anyone know of a (hopefully free) tool that can traverse a
    > > project and determine which "#include"s are not needed or needed
    > > in every .C file?  This would be helpful in removing header
    > > inclusions that are redundant and/or unnecessary.

    >
    > Use your editor.  Comment out a #include statement.  Recompile.  If
    > no errors appear, that statement can go.  If errors appear, remove
    > the comment.  Repeat.
    >


    That won't necessarily accomplish what the OP is trying to do. For
    example:
    /* Begin file: */
    #include <stdarg.h>

    int main()
    {
    puts("Hello, World");
    return EXIT_SUCCESS;
    }
    /* End of file */

    The above program will flag exactly 1 error, EXIT_SUCCESS not having
    been
    #define'd. #include'ing <stdlib.h> will fix that. The OP wants this
    tool to
    remind him to also #include <stdio.h>, and notify him that <stdarg.h>
    is
    unnecessary. Compiling with -Wall (or whatever it is on other
    compilers) will
    _warn_ me about the missing declaration for puts() (Which I may or may
    not remember
    is in <stdio.h>), but it won't make a peep about the extra header.

    Mr. Klein helpfully mentioned a tool which may help you.

    -- Marty Amandil
    Amandil, Sep 11, 2008
    #7
  8. C_guy

    CBFalconer Guest

    Amandil wrote:
    > CBFalconer <> wrote:
    >> C_guy wrote:
    >>
    >>> Does anyone know of a (hopefully free) tool that can traverse a
    >>> project and determine which "#include"s are not needed or needed
    >>> in every .C file? This would be helpful in removing header
    >>> inclusions that are redundant and/or unnecessary.

    >>
    >> Use your editor. Comment out a #include statement. Recompile.
    >> If no errors appear, that statement can go. If errors appear,
    >> remove the comment. Repeat.

    >
    > That won't necessarily accomplish what the OP is trying to do. For
    > example:
    > /* Begin file: */
    > #include <stdarg.h>
    >
    > int main()
    > {
    > puts("Hello, World");
    > return EXIT_SUCCESS;
    > }
    > /* End of file */
    >
    > The above program will flag exactly 1 error, EXIT_SUCCESS not
    > having been #define'd. #include'ing <stdlib.h> will fix that. The
    > OP wants this tool to remind him to also #include <stdio.h>, and
    > notify him that <stdarg.h> is unnecessary. Compiling with -Wall
    > (or whatever it is on other compilers) will _warn_ me about the
    > missing declaration for puts() (Which I may or may not remember
    > is in <stdio.h>), but it won't make a peep about the extra header.


    No, C_guy wants to know what he can delete. Your suggestion above
    will produce errors for the puts and the EXIT_SUCCESS usage.
    Removing the include of stdarg.h will not affect that.

    When something is undefined, most systems have an easy way to
    access help on that item, which will specify the include file to
    use. For example:

    puts
    ====

    Syntax
    ------
    #include <stdio.h>

    int puts(const char *string);

    and you can always search the C standard for the same information.
    The text version is very handy for this. Available (bzip2
    compressed) at:

    <http://cbfalconer.home.att.net/download/n869_txt.bz2>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 11, 2008
    #8
  9. On Thu, 11 Sep 2008 17:53:37 -0400, CBFalconer wrote:
    > Amandil wrote:
    >> CBFalconer <> wrote:
    >>> C_guy wrote:
    >>>
    >>>> Does anyone know of a (hopefully free) tool that can traverse a
    >>>> project and determine which "#include"s are not needed or needed in
    >>>> every .C file? This would be helpful in removing header inclusions
    >>>> that are redundant and/or unnecessary.
    >>>
    >>> Use your editor. Comment out a #include statement. Recompile. If no
    >>> errors appear, that statement can go. If errors appear, remove the
    >>> comment. Repeat.

    >>
    >> That won't necessarily accomplish what the OP is trying to do. For
    >> example:
    >> /* Begin file: */
    >> #include <stdarg.h>
    >>
    >> int main()
    >> {
    >> puts("Hello, World");
    >> return EXIT_SUCCESS;
    >> }
    >> /* End of file */
    >>
    >> [snip]

    >
    > No, C_guy wants to know what he can delete. [snip]


    Fine, then given

    /* Begin file: */
    #include <stdio.h>

    int main()
    {
    puts("Hello, World");
    return 0;
    }
    /* End of file */

    are you saying C_guy wants the inclusion of <stdio.h> to go?
    Harald van Dijk, Sep 11, 2008
    #9
  10. CBFalconer <> writes:
    [...]
    > and you can always search the C standard for the same information.
    > The text version is very handy for this. Available (bzip2
    > compressed) at:
    >
    > <http://cbfalconer.home.att.net/download/n869_txt.bz2>


    No, the C standard is not available as a text version.

    n869_txt.bz2 is a pre-standard draft. n1256.pdf is the latest
    post-standard draft, or the standard itself is available (but not
    free) via ANSI or ISO.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 11, 2008
    #10
  11. C_guy

    Richard Guest

    Keith Thompson <> writes:

    > CBFalconer <> writes:
    > [...]
    >> and you can always search the C standard for the same information.
    >> The text version is very handy for this. Available (bzip2
    >> compressed) at:
    >>
    >> <http://cbfalconer.home.att.net/download/n869_txt.bz2>

    >
    > No, the C standard is not available as a text version.
    >
    > n869_txt.bz2 is a pre-standard draft. n1256.pdf is the latest
    > post-standard draft, or the standard itself is available (but not
    > free) via ANSI or ISO.


    You must have mentioned this to Chuckles about 1000 times by now. Please
    stop replying to his SPAM and thus waking up threads he pollutes for the
    rest of us.
    Richard, Sep 11, 2008
    #11
  12. C_guy

    Flash Gordon Guest

    CBFalconer wrote, On 11/09/08 22:53:
    > Amandil wrote:
    >> CBFalconer <> wrote:
    >>> C_guy wrote:
    >>>
    >>>> Does anyone know of a (hopefully free) tool that can traverse a
    >>>> project and determine which "#include"s are not needed or needed
    >>>> in every .C file? This would be helpful in removing header
    >>>> inclusions that are redundant and/or unnecessary.
    >>> Use your editor. Comment out a #include statement. Recompile.
    >>> If no errors appear, that statement can go. If errors appear,
    >>> remove the comment. Repeat.

    >> That won't necessarily accomplish what the OP is trying to do. For
    >> example:
    >> /* Begin file: */
    >> #include <stdarg.h>
    >>
    >> int main()
    >> {
    >> puts("Hello, World");
    >> return EXIT_SUCCESS;
    >> }
    >> /* End of file */
    >>
    >> The above program will flag exactly 1 error, EXIT_SUCCESS not
    >> having been #define'd. #include'ing <stdlib.h> will fix that. The
    >> OP wants this tool to remind him to also #include <stdio.h>, and
    >> notify him that <stdarg.h> is unnecessary. Compiling with -Wall
    >> (or whatever it is on other compilers) will _warn_ me about the
    >> missing declaration for puts() (Which I may or may not remember
    >> is in <stdio.h>), but it won't make a peep about the extra header.

    >
    > No, C_guy wants to know what he can delete. Your suggestion above
    > will produce errors for the puts and the EXIT_SUCCESS usage.


    Not if the OP is compiling with C89, the most commonly implemented
    standard, or something close to C89 which is the most common default
    mode for compilers. In such a situation there is no diagnostic required
    for the call to puts. It is only with C99 the a diagnostic (which might
    not be an error) is required.

    > Removing the include of stdarg.h will not affect that.


    That was part of Amadil's point!

    > When something is undefined, most systems have an easy way to
    > access help on that item, which will specify the include file to
    > use. For example:


    True but irrelevant. There is no requirement on the commonly implemented
    standard to diagnose the lack of a declaration.

    > and you can always search the C standard for the same information.
    > The text version is very handy for this. Available (bzip2
    > compressed) at:
    >
    > <http://cbfalconer.home.att.net/download/n869_txt.bz2>


    Please at least tell people that is not any version of the standard and
    where they can find something that does actually represent the standard.
    It has been pointed out to you before that there are significant
    differences.
    --
    Flash Gordon
    Flash Gordon, Sep 12, 2008
    #12
  13. C_guy

    CBFalconer Guest

    Harald van D?k wrote:
    > CBFalconer wrote:
    >

    .... snip ...
    >
    >> No, C_guy wants to know what he can delete. [snip]

    >
    > Fine, then given
    >
    > /* Begin file: */
    > #include <stdio.h>
    >
    > int main()
    > {
    > puts("Hello, World");
    > return 0;
    > }
    > /* End of file */
    >
    > are you saying C_guy wants the inclusion of <stdio.h> to go?


    No, he wants to know if it CAN BE removed.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 12, 2008
    #13
  14. CBFalconer <> writes:

    > C_guy wrote:
    >>
    >> Does anyone know of a (hopefully free) tool that can traverse a
    >> project and determine which "#include"s are not needed or needed
    >> in every .C file? This would be helpful in removing header
    >> inclusions that are redundant and/or unnecessary.

    >
    > Use your editor. Comment out a #include statement. Recompile. If
    > no errors appear, that statement can go. If errors appear, remove
    > the comment. Repeat.


    That does not work if a pair (or more) of include files need each
    other but are (together) not needed by the rest of the code. This is
    poor design, but a robust method should be able to detect it.
    --
    Ben.
    Ben Bacarisse, Sep 12, 2008
    #14
  15. C_guy

    CBFalconer Guest

    Ben Bacarisse wrote:
    > CBFalconer <> writes:
    >> C_guy wrote:
    >>>
    >>> Does anyone know of a (hopefully free) tool that can traverse a
    >>> project and determine which "#include"s are not needed or needed
    >>> in every .C file? This would be helpful in removing header
    >>> inclusions that are redundant and/or unnecessary.

    >>
    >> Use your editor. Comment out a #include statement. Recompile. If
    >> no errors appear, that statement can go. If errors appear, remove
    >> the comment. Repeat.

    >
    > That does not work if a pair (or more) of include files need each
    > other but are (together) not needed by the rest of the code. This is
    > poor design, but a robust method should be able to detect it.


    I believe this is not allowed with standard C system headers.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Sep 12, 2008
    #15
  16. C_guy

    Guest

    On Sep 12, 1:12 am, Richard Heathfield <> wrote:
    > CBFalconer said:
    >
    > > Ben Bacarisse wrote:

    >
    > <snip>
    >
    > >> That does not work if a pair (or more) of include files need each
    > >> other but are (together) not needed by the rest of the code.  This is
    > >> poor design, but a robust method should be able to detect it.

    >
    > > I believe this is not allowed with standard C system headers.

    >
    > What is the basis for your belief?



    7.1.2 implies it. The standard headers are basically independent, can
    be included in any order, and can be included multiple times, without
    altering their meaning. A minor except exists for assert.h.

    But I don’t think the OP was limiting the discussion to the standard
    headers.
    , Sep 12, 2008
    #16
  17. C_guy

    jacob navia Guest

    C_guy wrote:
    > Does anyone know of a (hopefully free) tool that can traverse a
    > project and determine which "#include"s are not needed or needed in
    > every .C file? This would be helpful in removing header inclusions
    > that are redundant and/or unnecessary.
    >
    > Thanks!


    The lcc-win compiler will output all files included
    in a given source file using the

    lcc -M

    option. If you collect this output in a series of files
    you will (implicitely) obtain which files are NOT needed
    since they will not appear in that list.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Sep 12, 2008
    #17
  18. On 11 Sep, 01:09, CBFalconer <> wrote:
    > C_guy wrote:


    > > Does anyone know of a (hopefully free) tool that can traverse a
    > > project and determine which "#include"s are not needed or needed
    > > in every .C file?  This would be helpful in removing header
    > > inclusions that are redundant and/or unnecessary.

    >
    > Use your editor.  Comment out a #include statement.  Recompile.  If
    > no errors appear, that statement can go.  If errors appear, remove
    > the comment.  Repeat.


    [-] Search for #include: (40261 results found in 9365 files)

    --
    Nick Keighley
    Nick Keighley, Sep 12, 2008
    #18
  19. jacob navia <> writes:
    > C_guy wrote:
    >> Does anyone know of a (hopefully free) tool that can traverse a
    >> project and determine which "#include"s are not needed or needed in
    >> every .C file? This would be helpful in removing header inclusions
    >> that are redundant and/or unnecessary.
    >> Thanks!

    >
    > The lcc-win compiler will output all files included
    > in a given source file using the
    >
    > lcc -M
    >
    > option. If you collect this output in a series of files
    > you will (implicitely) obtain which files are NOT needed
    > since they will not appear in that list.


    I don't think that addresses the OP's question.

    A concrete example:

    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    puts("Hello, world");
    return 0;
    }

    The OP is looking for a tool that will tell him that the
    "#include <math.h>" is not needed.

    lcc -M, if I understand it correctly, can tell you which headers are
    not #include'd; it can't tell you which headers are #include'd
    unnecessarily.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 12, 2008
    #19
  20. C_guy

    Ian Collins Guest

    Keith Thompson wrote:
    > jacob navia <> writes:


    >> The lcc-win compiler will output all files included
    >> in a given source file using the
    >>
    >> lcc -M
    >>
    >> option. If you collect this output in a series of files
    >> you will (implicitely) obtain which files are NOT needed
    >> since they will not appear in that list.

    >
    > I don't think that addresses the OP's question.
    >
    > A concrete example:
    >
    > #include <stdio.h>
    > #include <math.h>
    > int main(void)
    > {
    > puts("Hello, world");
    > return 0;
    > }
    >
    > The OP is looking for a tool that will tell him that the
    > "#include <math.h>" is not needed.
    >
    > lcc -M, if I understand it correctly, can tell you which headers are
    > not #include'd; it can't tell you which headers are #include'd
    > unnecessarily.
    >

    Like this:

    lint -Nlevel /tmp/x.c

    include file is unnecessary
    (2) /usr/include/math.h

    --
    Ian Collins.
    Ian Collins, Sep 12, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dont bother
    Replies:
    0
    Views:
    792
    dont bother
    Mar 3, 2004
  2. Phil
    Replies:
    4
    Views:
    667
    Gabriel Genellina
    Jan 17, 2010
  3. Ian
    Replies:
    2
    Views:
    1,938
  4. Steve Mauldin
    Replies:
    0
    Views:
    132
    Steve Mauldin
    Feb 6, 2004
  5. Peña, Botp
    Replies:
    1
    Views:
    231
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page