include cleanup

Discussion in 'C Programming' started by Florian Kaufmann, Jan 23, 2008.

  1. Hello

    Is there a tool that helps to keep the list of include directives
    clean and tidy? I would like that each header/source file has only the
    includes that it really needs, and that forward declarations are used
    instead of includes where possible.
    I can imagine that this task is impossible to solve in general, but I
    hope there are tools that get the job at least partially done.

    Flo
    Florian Kaufmann, Jan 23, 2008
    #1
    1. Advertising

  2. Florian Kaufmann wrote:
    > Is there a tool that helps to keep the list of include directives
    > clean and tidy? I would like that each header/source file has only the
    > includes that it really needs, and that forward declarations are used
    > instead of includes where possible.
    > I can imagine that this task is impossible to solve in general, but I
    > hope there are tools that get the job at least partially done.


    I call it "the combination of your text editor and the compiler".
    Start by removing all includes and then include only those that are
    needed.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 23, 2008
    #2
    1. Advertising

  3. > I call it "the combination of your text editor and the compiler".
    > Start by removing all includes and then include only those that are
    > needed.


    Yes, that is surely possible. However, the automated approach has the
    advantage that the work is done by someone else (the tool) and I can
    concentrate on the important stuff. I imagine it quite time consuming
    to do the manual approach every half year or so for all the hundredths
    source/header files.
    Florian Kaufmann, Jan 23, 2008
    #3
  4. In article <>,
    Florian Kaufmann <> wrote:
    >Hello
    >
    >Is there a tool that helps to keep the list of include directives
    >clean and tidy? I would like that each header/source file has only the
    >includes that it really needs, and that forward declarations are used
    >instead of includes where possible.


    [OT]
    The latter part of that is not recommended. If you have the same
    function declaration in multiple files, then when you change
    the declaration, you will have to go around to each file that
    textually declares it and modify the code. That process is error
    prone; it is pretty much inevitable that the declarations *will*
    get out of sync. If, however, you #include the declaration of
    the header, then you only need to modify one place, the #include
    file, after which the compiler will be able to catch any
    now-incompatible calls to the function.
    --
    "History is a pile of debris" -- Laurie Anderson
    Walter Roberson, Jan 23, 2008
    #4
  5. Florian Kaufmann

    anon Guest

    Florian Kaufmann wrote:
    > Hello
    >
    > Is there a tool that helps to keep the list of include directives
    > clean and tidy? I would like that each header/source file has only the
    > includes that it really needs, and that forward declarations are used
    > instead of includes where possible.
    > I can imagine that this task is impossible to solve in general, but I
    > hope there are tools that get the job at least partially done.
    >
    > Flo


    try this:
    http://www.stolk.org/idep-0.5.tar.gz
    anon, Jan 23, 2008
    #5
  6. > The latter part of that is not recommended. If you have the same
    > function declaration in multiple files, then when you change
    > the declaration, you will have to go around to each file that
    > textually declares it and modify the code. That process is error


    You are right - I wasn't specific enough. I am aware of the general
    rule that any kind redundancy is to be avoided. What I meant are the
    kind of forward declarations that are often done in C++ like

    class A;
    class B { A& a;};

    Flo
    Florian Kaufmann, Jan 23, 2008
    #6
  7. Florian Kaufmann

    James Kanze Guest

    On Jan 23, 3:20 pm, "Victor Bazarov" <> wrote:
    > Florian Kaufmann wrote:
    > > Is there a tool that helps to keep the list of include directives
    > > clean and tidy? I would like that each header/source file has only the
    > > includes that it really needs, and that forward declarations are used
    > > instead of includes where possible.
    > > I can imagine that this task is impossible to solve in general, but I
    > > hope there are tools that get the job at least partially done.


    > I call it "the combination of your text editor and the
    > compiler". Start by removing all includes and then include
    > only those that are needed.


    That will usually result in too many includes being used---if
    the includes A.hh "incidentally" includes B.hh, for example,
    this technique will result in your removing the include of B.hh,
    even if you need it. From experience, I know that I often have
    to add includes that I've forgotten when moving between
    different implementations of the standard library.

    Also, it doesn't consider the possibility of using a forward
    declaration, rather than an include.

    What you really need is a tool which looks at every symbol in
    the file, and determines where it was defined, and whether a
    complete type was needed or not. For symbols for which a
    complete type is required, it outputs the name of the file in
    which the type was defined; for the others, if the type is a
    class type, it outputs a simple "class T;", with the name of the
    file where the symbol was defined in comments. (The user must
    then intervene to determine whether the symbol is guaranteed to
    be a class, or whether future evolution might replace the class
    with e.g. a typedef to a template instantiation. In the latter
    case, you need the include, even if the forward declaration
    works at present.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 24, 2008
    #7
  8. Florian Kaufmann

    Billy Bong Guest

    On Wed, 23 Jan 2008 01:14:14 -0800, Florian Kaufmann wrote:

    > Hello
    >
    > Is there a tool that helps to keep the list of include directives clean
    > and tidy? I would like that each header/source file has only the
    > includes that it really needs, and that forward declarations are used
    > instead of includes where possible.
    > I can imagine that this task is impossible to solve in general, but I
    > hope there are tools that get the job at least partially done.
    >
    > Flo


    PC-lint will do this.

    http://www.gimpel.com/

    --
    Billy
    Billy Bong, Jan 24, 2008
    #8
    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. Dan Walls
    Replies:
    3
    Views:
    1,525
    Alvin Bruney [MVP]
    Apr 16, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    ADSI Cleanup Help!

    =?Utf-8?B?VGltOjouLg==?=, Mar 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    =?Utf-8?B?VGltOjouLg==?=
    Mar 30, 2005
  3. VB Programmer
    Replies:
    3
    Views:
    487
    VB Programmer
    Feb 10, 2006
  4. Florian Kaufmann

    include cleanup

    Florian Kaufmann, Jan 23, 2008, in forum: C++
    Replies:
    7
    Views:
    334
    Billy Bong
    Jan 24, 2008
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    875
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page