COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Sutters guidelines and redundant include guards

• Email
• Follow

I have just come across the guideline: "Always write internal #include
guards. Never write external #include guards." and I have a few

1. Sutter's example is not quite correct. The code fragment he gives is
for headers that do not have a predictable guard (e.g system headers or
third-party headers). When the guard is predictable the construct is
(slightly) smaller, e.g:

#ifndef INCLUDED_FOOBAR_H
#include "foobar.h"
#endif

2. Microsoft compilers and other compilers aimed at Windoze machines do
indeed make redundant include guards, er, redundant. However, Unix
compilers have a long way to go before they catch up in this area.
There maybe perhaps could still be some benefit from using them for
code that has to compile on Unix.

3. IMO the predictable aspect of the include guard has been glossed
over, leaving the impression that the redundant guard construct is
high-maintainance. Well it would be if the guard was not predictable.
But it is predictable, using whatever rules are established in the
corporate coding stds. This reduces the maintainance costs and leaves
one with what is probably the main objection: the code is longer and
more ugly. This is still an understandable reason for avoiding them but
is less strong than Sutter's guideline suggests.

4. Given the above, whether to use them or not boils down to whether or
not the improved builds times is a gain that is worth the cost of the
code uglification. This would have to be measured. Given the
predictable mechanical nature of the redundant guards surely there must
be a tool by now that would convert to use redundant guards, then we
could measure the improvement. So the guideline seems to come down a
little heavy on a measure that could have some benefit.
Regards,

Andrew Marlow

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0
Reply apm35 (156) 12/14/2004 7:54:23 PM

See related articles to this posting

apm35@student.open.ac.uk wrote:
> 1. Sutter's example is not quite correct. The code fragment he
> gives is for headers that do not have a predictable guard (e.g
> predictable the construct is (slightly) smaller, e.g:
>
> #ifndef INCLUDED_FOOBAR_H
> #include "foobar.h"
> #endif

Why would you need to write the external guard, though? Maybe you
weren't aware of the optimizations done by UNIX compilers?

> 2. Microsoft compilers and other compilers aimed at Windoze
> machines do indeed make redundant include guards, er, redundant.
> However, Unix compilers have a long way to go before they catch
> up in this area. There maybe perhaps could still be some benefit
> from using them for code that has to compile on Unix.

Unfortunately, #pragma once is not standard, and it's not just an
optimization. Often, including multiple times generates wrong code,
like violating the ODR. You still need the redundant include guards for
portable code.

Most UNIX compilers have their own optimization, instead of #pragma
once, and it is better. They recognize the structure of a redundant
include guard, and will not read the file multiple times, as they
understand it's a no-op. This fails gracefully on compilers that don't
support the optimization.

However, Windows compilers tend not to support the optimization, so
there is a lot of code like this,

#ifndef SOMEFILE_H
#define SOMEFILE_H
#ifndef LACKS_PRAGMA_ONCE
#pragma once
#endif // !LACKS_PRAGMA_ONCE
// ...
#endif // !SOMEFILE_H

I believe ACE starts every hearder with a construct like that. It does
work though, and it should eliminate any motive for external include
guards unless you're using a terribly dated compiler.

--
Dave O'Hearn

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

apm35@student.open.ac.uk wrote:
> I have just come across the guideline: "Always write internal #include
> guards. Never write external #include guards." and I have a few
>
> 1. Sutter's example is not quite correct. The code fragment he gives is
> for headers that do not have a predictable guard (e.g system headers or
> third-party headers). When the guard is predictable the construct is
> (slightly) smaller, e.g:
>
> #ifndef INCLUDED_FOOBAR_H
> #include "foobar.h"
> #endif
>
> 2. Microsoft compilers and other compilers aimed at Windoze machines do
> indeed make redundant include guards, er, redundant.

Assuming you don't include the a header file through two different
paths that it doesn't realise are the same - though that would be
rather silly.

> However, Unix compilers have a long way to go before they catch up
> in this area.
<snip>

GCC's preprocessor recognises the ifndef...#define...#endif pattern
#and can skip the whole file if it's included again.  Any other
implementer is free to do the same.  There's no need for a
non-standard extension, or the external guards.  This also avoids the
problem of having to know for certain whether two paths point to the
same file.

--
Ben Hutchings
compatible: Gracefully accepts erroneous data from any source

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

I know that not every platform supports #pragma once. But does anybody
know of a platform where it does something undesirable?

IMO, warnings would count as undesirable if there was no easy way to
supress them. But according to the standard (16.6):
Any pragma that is not recognized by the implementation is ignored.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

Ben Hutchings wrote:

> GCC's preprocessor recognises the ifndef...#define...#endif pattern
> #and can skip the whole file if it's included again.  Any other
> implementer is free to do the same.  There's no need for a
> non-standard extension, or the external guards.  This also avoids the
> problem of having to know for certain whether two paths point to the
> same file.

It's worth noting that GCC has done this for quite some time now, at
least ten years.  It's surprising that so few other compilers have

--
James Kanze           GABI Software         http://www.gabi-soft.fr
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

Allan W wrote:
> I know that not every platform supports #pragma once. But does
> anybody know of a platform where it does something undesirable?
>
> IMO, warnings would count as undesirable if there was no easy
> way to supress them. But according to the standard (16.6):
> Any pragma that is not recognized by the implementation is
> ignored.

I put it in the !LACKS_PRAGMA_ONCE test because older versions of gcc
issue a warning that "#pragma once' is obsolete". It's annoying, and I
don't know how to disable it. gcc 3.4 has correct support for pragma
once and doesn't issue the warning.

Both gcc and MSVC++ issue warnings on unrecognized pragmas, but they
both have ways to disable them. In general though, I just find pragmas
scary, and I like to #ifdef them.

--
Dave O'Hearn

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

Dave O'Hearn wrote:
> including multiple times generates wrong code,
> like violating the ODR. You still need the redundant include guards
for
> portable code.

I think you are confusing redundant include guards with conventional
include guards. Conventional include guards are used to protect againts
harmful results if a file is included multiple times in a TU. This is
done by the file beginning with #ifndef guardname #define guardname
blah-blah #endif. This protects against ODR etc. Redundant include
guards are an ADDITIONAL measure where the include guard macro is
tested to ensure that the #include is not performed if the inclusion

With conventional include guards the header may be parsed many times
but each time other than the first is effectively a no-op. However, the
use of conventional include guards without the additional measure of
redundant include guards means that extra work is done in finding and
opening the file and parsing its contents (albeit the contents will be
quickly scanned until the closing #endif is encountered).

>
> Most UNIX compilers have their own optimization, instead of #pragma
> once, and it is better. They recognize the structure of a redundant
> include guard,

They recognise the construct of a *conventional* include guard. And so
the cost of finding and opening the file (which is large compared to
the cost of skipping to the closing #endif) is still incurred.

> work though, and it should eliminate any motive for external include
> guards unless you're using a terribly dated compiler.

One of my points was that commercial unix compilers are not exactly
leading edge and so do not (AFAIK) have any work done in this area.
-Andrew M.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

 0

apm35@student.open.ac.uk wrote:
> Dave O'Hearn wrote:
>> including multiple times generates wrong code,
>> like violating the ODR. You still need the redundant include guards
>> for portable code.
<snip>
> With conventional include guards the header may be parsed many times
> but each time other than the first is effectively a no-op. However, the
> use of conventional include guards without the additional measure of
> redundant include guards means that extra work is done in finding and
> opening the file and parsing its contents (albeit the contents will be
> quickly scanned until the closing #endif is encountered).
<snip>

A smart preprocessor can identify the guard macro the first time a
file is included and check it the second time before opening the file.
(It is reasonable to assume that source files are not modified during
compilation.)  This is what GCC's preprocessor does. See
<http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libcpp/files.c?rev=1.1.2.3>
and in particular the function should_stack_file.

--
Ben Hutchings
This sentence contradicts itself - no actually it doesn't.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]
`
 0

7 Replies
188 Views

Similar Articles

12/10/2013 10:31:38 PM
page loaded in 161041 ms. (0)

Similar Artilces:

Errors with including tables
I'm defending my dissertation soon, and am taking tables and figures features in individual essays and trying to include them into the dissertation, proper. I have never had problems including figures and tables with the previous essays. They format fine, so I know there are no problems in the actual tables or figures. Rather, it seems like something with my school's style sheet (which we're required to use) that I'm not catching. I'm getting errors when it attempts to produce the pdf. The name of the school's stylesheet is uga.sty. Here's my preamble, followed...

Is REXX included in AIX?
or is it purchased separately? Thanks, Neal "... or is a free download?" ;-) https://sourceforge.net/projects/oorexx -- Michael Lueck Lueck Data Systems http://www.lueckdatasystems.com/ Neal Eckhardt wrote: > or is it purchased separately? > It's included, but an "Optional Install." -- Jaime A. Cruz Secretary Nassau Wings Motorcycle Club http://www.nassauwings.org/ Member-at-Large AMA District 34 http://www.AMADistrict34.com/ Pop's Run http://www.popsrun.org/ On Sat, 19 Jan 2008 20:23:55 GMT, "Cruz, Jaime" <Spammers@Bite.Me> wrote:...

module_function and Object include.
I understand that the use of the Module's class method "module_function" is to - create class method copies of the instance methods defined in module - make those instance methods "private" when mixed in. e.g. 1 module Functions 2 module_function 3 def greet(whom) 4 puts "Hi, #{whom}" 5 end 6 end Now, if I included this module in my class like: class Greeter include Functions end everything works as expected. But, if I opened up the Object class and included Function there e.g. class Object include Functions end ...

Hi, Is it a problem if I have an executable and a library use different versions of another library. Example: If I have an executable: A.exe, and it basically wraps and depends on a static library A.lib Both A.exe and A.lib need another library B.lib If I have a situation like this: The A.lib library includes B.lib version 1 (uses header files from this library) The A.exe executable includes B.lib version 2 The A.exe executable links against B.lib version 2 Under what conditions would this be problematic? Thanks On Aug 26, 1:10=A0pm, shar33oob <shar...@googlem...

I havea pix 501 and a managed T1 comming into the building. I need something incase that T1 drops. It can be as advanced as auto failover, or as simple as me calling someone up at the branch and having them move a cat5 cable from one device to another. I was thinking of HSRP, but I would need access to both the routers and the people who manage the T1 now said they will not setup HSRP with us, and if we want to do that I need to change the service to us managing it. We were thinking of a dsl circuit for the backup link, I just need to find a reliable way to make it kick over to that if/whe...

include files for bison?
I am working on a large project with multiple parsers for several different XML files. Some of the elements and types in the XML schemas are identical, and I would like to share some of the parser code. I am working in flex/bison and am wondering if there is a way to use include files for bison. I understand that I can use #include in the C code section, but this is not what I am talking about. I want to set aside some repeated grammar rules into a separate file and then "paste" that file into several bison parsers via an include statement. Is this sort of thing possible? How would...

redundant multiple inheritance
Here's an easy one for you Matlab experts out there. In the new object model, how is redundant multiple inheritance handled, i.e. I have three classes: classdef A < Base classdef B < A classdef C < Base, B How is dispatch from C handled? is it different if the classdef were: classdef C < B, Base ? ...

Include figure in Article
Hi everybody; I want to include a picture top of the first page (left or right it is not important). After this picture; title, authors name and abstrac must be come. Also this picture only arise in first page not after this. It is possible Thanks in advance Firat On 3 Ocak, 14:03, F=C4=B1rat <agimfi...@gmail.com> wrote: > Hi everybody; > > I want to include a picture top of the first page (left or right it is > not important). After this picture; title, authors name and abstrac > must be come. Also this picture only arise in first page not after &...

anyone aware of a cheap method of load balancing, with it taking downed servers out of rotation? not with things like LVS and the such. -- Anyone who becomes master of a city accustomed to freedom and does not destroy it may expect to be destroyed by it; for such a city may always justify rebellion in the name of liberty and its ancient institutions. -Niccolo Machiavelli In article <1034dvt52222hbc@corp.supernews.com>, fugi <fugi@ultra.bl.org> wrote: > anyone aware of a cheap method of load balancing, with it taking > downed servers out of rotation? not with things lik...

import vs include
So the difference between import and include is that: import = used for binary library like DLL or .Lib files. it's very similar to include that it load all the header (function definition) from the DLL file so that we can use the header file just like include include = just include header (source file) ========================== #import "c:\Program Files\Common Files\System\ADO\msado15.dll" \ rename("EOF", "EndOfFile") worlman385@yahoo.com wrote: > So the difference between import and include is that: There is no "import" in S...

How to create JS includes
Hello, This should be a simple question to anyone who knows JavaScript, but I'm new to it. I have a JavaScript that I need to use in many pages. How do I create JS includes? I tried the standard method - created a text file, then includud it like this: <!-------------#Include File="includes/js.txt"-------------------> but this did't work for me. I would appreciate your advice. Thank you, -- Peter Afonin Peter this is how: <html> <head> <title>Test!</title> <!-- now include script tag with src attribute pointing to --> <!-- t...

Bigger projects, including files?
if i have a larger project and want to divide my program into several files, how do i include these files in the mainprogram? using import someprojectfile doesnt work because import is for site- packages right and i dont want to put all my files in that folder. so how do i do it? globalrev wrote: > if i have a larger project and want to divide my program into several > files, how do i include these files in the mainprogram? > > using import someprojectfile doesnt work because import is for site- > packages right and i dont want to put all my files > in that folder. > ...

Q1. reducing grammar redundancy
Call me an optimization paranoid but...I have a question regarding the issue of grammar generalization to be used in language tools. [1] I'm familiar with several compiler compilers. The issue is in grammar rules organization. In the desrcribing languages like LISP and XML it is appealing not to mention they have some general "element" pattern. How can I benefit from this fact eliminating the redundancy: sqrt-> LPAREN "sqrt" NUM RPAREN; // LISP element synthax plus -> LPAREN "plus" NUM NUM RPAREN; Is there a known technique to define a basic &q...

Rake FileList Includes Request
I would like to make a request for a Rake feature. When using FileList directives, such as the rdoc task's #include (I assume it is a FileList), it would be nice if it didn't throw an error for files it could not find, a warning should, I think, suffice. Thanks, T. T. Onoma said: > I would like to make a request for a Rake feature. When using FileList > directives, such as the rdoc task's #include (I assume it is a FileList), > it > would be nice if it didn't throw an error for files it could not find, a > warning should, I think, suffice. FileList itsel...

Including figures using WinEdt
Hi, I'm very new to TeX and am having some trouble including figures in my document. I have the following headers: \documentclass{report} \usepackage{graphicx} And when I attempt to insert a figure, my code is: \begin{figure}[hr] \begin{center} \includegraphics[height=2in]{2-10.eps} \end{center} \end{figure} The file 2-10.eps is in the same directory as my .tex file. When I compile it in WinEdt (using MikTeX) by clicking the LaTeX button, it seems to compile fine. I can then view the dvi file, which looks ok - except that there it's blank where the figures can be. Can anyone h...

Guidelines for instruction set design?
Hello, I'm currently trying to port LCC to a custom CPU implemented in a FPGA. A different person is designing the hardware and instruction set. I was wondering if there are some general guidelines one should observe when designing an instruction set so that a C compiler can easily be ported to that CPU. I'm asking because I'm having a hard time porting LCC with the current instruction set of our custom CPU. I've already searched in this group's archive but I couldn't find an answer. I can provide more information on our current instruction set if it helps you ans...

Javascript include not working in Safari?