finding redundant #includes to shorten compile time

B

Benjamin Rutt

Are there any C tools that can find redundant #includes in a project,
so as to shorten compile time? Of course, eliminating single
#includes by hand and determining if the recompile fails is one
option, though that is an extremely manual and time-intensive
approach. Thanks,
 
M

Mark A. Odell

Are there any C tools that can find redundant #includes in a project,
so as to shorten compile time? Of course, eliminating single
#includes by hand and determining if the recompile fails is one
option, though that is an extremely manual and time-intensive
approach. Thanks,

This trick will eliminate including the same file multiple times per
compile:

/* foo.h
*/
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED

/* foo.h file contents
*

#endif /* FOO_H_INCLUDED */
 
B

Benjamin Rutt

Mark A. Odell said:
This trick will eliminate including the same file multiple times per
compile:

/* foo.h
*/
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED

/* foo.h file contents
*

#endif /* FOO_H_INCLUDED */

Thanks, I'm sorry, I knew of that trick actually, and I've rarely seen
a header file without it...I guess I misstated my question. I was
actually talking about #includeing files that you don't need at all,
not even once, so my using 'redundant' was incorrect. I should have
asked:

How do you find which header files aren't used at all by a single
compilation unit, and can safely be deleted from a the .c file?
 
G

gabriel

Benjamin said:
How do you find which header files aren't used at all by a single
compilation unit, and can safely be deleted from a the .c file?

I believe this falls under the topic of "lint," such as declaring unused
variables, code blocks that do nothing (ie "if (0 == 1) {[...]}", etc...),
redundant stuff ("a = 1; a = 5"), and all the other junk that accumulates
as projects evolve.

I would use a lint checker for that.

I have used the Gimpel lint checker (www.gimpel.com) quite successfully,
I'm sure there are others including free ones.
 
T

Thomas Matthews

Mark said:
This trick will eliminate including the same file multiple times per
compile:

/* foo.h
*/
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED

/* foo.h file contents
*

#endif /* FOO_H_INCLUDED */

The file opening time may be reduced by using:
#ifndef FOO_H_INCLUDED
#include "foo.h"
#endif

Finding and opening a file is one of the major items
for compilation times. Also, in Mark's version, the
compiler must scan all the code looking for the
#endif, which takes time.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mark A. Odell

The file opening time may be reduced by using:
#ifndef FOO_H_INCLUDED
#include "foo.h"
#endif

Finding and opening a file is one of the major items
for compilation times. Also, in Mark's version, the
compiler must scan all the code looking for the
#endif, which takes time.

Doesn't this approach become cumbersome and error prone with many source
files and many header files? The standard way I mentioned means that the
user of the header file can blissly include the header file without fear
of multiple definition warnings.
 
J

Jack Klein

Are there any C tools that can find redundant #includes in a project,
so as to shorten compile time? Of course, eliminating single
#includes by hand and determining if the recompile fails is one
option, though that is an extremely manual and time-intensive
approach. Thanks,

PC-Lint. http://www.gimpel.com.

Worth every penny, once you get the configuration files set up for
your code.
 
E

E. Robert Tisdale

Thomas said:
The file opening time may be reduced by using:

#ifndef FOO_H_INCLUDED
#include "foo.h"
#endif

Finding and opening a file is one of the major items
for compilation times. Also, in Mark's version, the
compiler must scan all the code looking for the
#endif, which takes time.

No!

The C preprocessor remembers the names of *idempotent* header files
and will *not* attempt to find, open or read them a second time.
Your suggestion is out-of-date and no longer necessary or useful.
 
K

Keith Thompson

E. Robert Tisdale said:
No!

The C preprocessor remembers the names of *idempotent* header files
and will *not* attempt to find, open or read them a second time.
Your suggestion is out-of-date and no longer necessary or useful.

No, some implementations of the C preprocessor do this. I have no
idea how many, but it's unwise to assume that they all do. (Not that
it matters, since there's no real functional difference.)
 
G

gabriel

E. Robert Tisdale said:
The C preprocessor remembers the names of *idempotent* header files
and will *not* attempt to find, open or read them a second time.
Your suggestion is out-of-date and no longer necessary or useful.

Cammon, you know better than that! Of course, _some_ implementations might
do this, but then if you want to bring up implementation-specific features,
then I could chime in and say we're all fool for discussing this topic
given that precompiled headers exist.
 
E

E. Robert Tisdale

Keith said:
No, some implementations of the C preprocessor do this.
I have no idea how many, but it's unwise to assume that they all do.
(Not that it matters, since there's no real functional difference.)

It is *unwise* to cobble your code
just to accommodate an inferior C preprocessor.
If your C preprocessor does not implement this optimization,
it's time to shop around for a better C compiler.
 
D

Default User

gabriel said:
Cammon, you know better than that! Of course, _some_ implementations might
do this, but then if you want to bring up implementation-specific features,
then I could chime in and say we're all fool for discussing this topic
given that precompiled headers exist.


You have to understand that Trollsdale believes his implementation is
the only one in the world. So anything true for it is true for all.



Brian Rodenborn
 
R

Richard Heathfield

E. Robert Tisdale wrote:

It is *unwise* to cobble your code
just to accommodate an inferior C preprocessor.
If your C preprocessor does not implement this optimization,
it's time to shop around for a better C compiler.

Some platforms have a limited number of C implementations (e.g. just one or
two). If none of the implementations available for a particular platform
implement the feature you desire, what would you recommend? Changing the
platform?
 
G

gabriel

Richard said:
platform implement the feature you desire, what would you recommend?
Changing the platform?

Write your own compiler, like real men do!

Just kidding...
 
D

Dan Pop

In said:
Are there any C tools that can find redundant #includes in a project,
so as to shorten compile time? Of course, eliminating single
#includes by hand and determining if the recompile fails is one
option, though that is an extremely manual and time-intensive
approach. Thanks,

The easiest approach is not to include unnecessary headers in the first
place.

Anyway, unless they reside on a particularly slow medium, headers increase
the compilation time by an insignificant amount.

Dan
 
C

CBFalconer

Richard said:
E. Robert Tisdale wrote:



Some platforms have a limited number of C implementations (e.g.
just one or two). If none of the implementations available for a
particular platform implement the feature you desire, what would
you recommend? Changing the platform?

Hell no. Simply advise Trollsdale and he will have a compiler
ready for you in the next 48 hours. It will have excellent
specifications. A minor problem might be that it won't meet them,
and that it reports success or failure by crashing. A
compensation may be that it is subject to buffer overruns, so with
a bit of practice you can write a source that patches it on the
fly.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top