Re: include file question



On Fri, 20 Mar 2009 09:06:43 -0400, "RB" <NoMail@NoSpam> wrote:

I'm a novice hobby programmer and I've run across something I'm
confused on. First off I first read that you could keep the compiler
from including a header file twice with the #pragma once
But then I read that #pragma once is obsolete ? I wonder
why since it seems so much less typing than,
#ifndef FILE_FOO_SEEN
#define FILE_FOO_SEEN
THE ENTIRE FILE
#endif /* FILE_FOO_SEEN */
And then I read about #import , which is another way to include
a file but it also says it is obsolete and has special issues that
#include does not. So in addition to any info you can give me as to
the above items, I have the following questions below.

The #include guards are portable since the beginning of time, and provided
they are unique, foolproof. The #pragma was invented later as a typing
shortcut and to speed preprocessing by avoiding repeated header parsing
just to match the #ifndef/#endif guards, but compilers such as gcc
deprecated the #pragma once it was realized that it wasn't foolproof,
wasn't portable, and that the guard idiom could be specially recognized to
obtain the speed benefit of the #pragma. Compared to the macro guards, the
#pragma is more error-prone, because it depends on the compiler being able
to canonicalize filenames in a unique way. For example, the compiler has to
be able to recognize these are the same files:

//server/share/filename.h
C:/include/filename.h

It has to be able to do this even for hard/soft links, and, of course, it
must not consider two different filename.h files that exist in different
physical paths identical. OTOH, it is possible (if unwise) to have two
identical filename.h files existing along different physical paths, and
#pragma once will incorrectly consider them different files. The macro
guards handle this correctly.

1.How would one test to see if in fact an include file was included twice
by the compiler.

One wouldn't, because it would be prevented by the use of #pragma once or
include guards. Note that some headers use neither because they are
designed to be #included more than once, such as <assert.h>, whose behavior
depends on the definition of NDEBUG prior to its #inclusion, e.g. the
following is legal (if a little strange):

#define NDEBUG
#include <assert.h>
.... asserts are disabled
#undef NDEBUG
#include <assert.h>
.... asserts are enabled

2.What exactly are the problems with an include file being included twice?

Among other things, it is illegal to define the same class twice in the
same translation unit.

3.Prior to this time I had really not thought about this and I have compiled
several apps with includes of the same file in more than one cpp file with
seemingly no errors or warnings. Do the current compilers check for this
on their own?

That isn't a problem. Each .cpp file is the basis of a translation unit,
which is the output of the preprocessor after all #include directives have
been processed, macros expanded, and so forth. The separate compilation
model of C++ demands that each translation unit be, well, compiled
separately, which means each one #includes the headers it needs. This is
not a problem unless you somehow break the one-definition rule (ODR),
which, among other things, requires that a class have the same members,
layout, and so forth everywhere in the program. (Most compilers will *not*
detect ODR violations.) A more common issue is compilation speed, and
precompiled headers were invented to help overcome the inefficiency of
loading and parsing the same headers in the many files that comprise the
typical program.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: include file question
    ... Where did you read that #pragma once is obsolete? ... because it suggests that what you need is some kind of weird compiler hack to make your ... inside the header file to prevent multiple execution of the contents. ... documentation about Objective C, which is a language which is NOT the C++ language, has ...
    (microsoft.public.vc.mfc)
  • Re: Why is GetStoreInfo() returning nonsensical values?
    ... compiler or #pragma in your headers? ... Paul T. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Ada checks suppression thanks to compilation options and Ada conformity
    ... One text book describes "pragma" as a "directive to the ... the compiler -- and indicate that compilers are allowed to ignore ... But this doens't apply to pragma Suppress, ... specify a compiler command-line option to suppress all checks, ...
    (comp.lang.ada)
  • pragma Pathway - optimising optimizing profiling
    ... I'd like to ask people's comments about another pragma I intend to ... code based on the gathered profiling data. ... rather in terms of some intermediate form derived by the compiler ... executional pathway through the program by name. ...
    (comp.lang.ada)
  • Re: Bounds checked arrays
    ... > Nobody is perfect. ... case the compiler is progammed by humans. ... I would not support this proposal with the #pragma ... If the source file was modified, ...
    (comp.lang.c)

Loading