Re: if !defined and #pragma once

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The #if !defined() sequence is what is called an "include guard" and was, for decades, the
standard Best Practice to allow you to create header files that could be included multiple
times.

The only problem was that it was inefficient. With VS6, Microsoft added the #pragma once
construct, but since code had to be compiled with older compilers (VS4.2, VS5), the older
include-guards had to remain in place.

Starting with VS.NET, this older support was dropped, and newer wizards only put in the
#pragma once. If you have code that needs to be shared across platforms, however, you
will want to use include-guards as well (this generally applies to, say, math, image, or
signal-processing code that might run on the Mac, Unix, Linux, etc. as well). Otherwise,
as you observe, they are redundant.
joe

On Tue, 01 Apr 2008 18:18:36 +0800, asm23 <asmwarrior@xxxxxxxxx> wrote:

Hi,I'm a visual c++ 6.0 user. I'm confused that the CPP file generated
by the IDE always have these statement below:

#if!defined(AFX_XXXXX_H__32583F3A_E701_4274_AFD6_426BC1C59E81__INCLUDED_)
#define AFX_XXXXX_H__32583F3A_E701_4274_AFD6_426BC1C59E81__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

So, my question is "#if! defined (XXXX...)" is really useless? as the
newer compiler such as VS.NET 2001 VC.NET 2003...2008 all support the
#pragma once. the "if! ..." is functionally redundant.Is it right? Thanks.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: C is too old? opinions?
    ... You insisted that this pragma is supported by many compilers, ... I cannot find any mention of it in the documentation for my current gcc ... If they support if, it is undocumented and probably deprecated. ... OS platforms and all the mainstream compilers used this pragma I know I ...
    (comp.lang.c)
  • Re: Correspondence between headers and macros
    ... So Norcroft doesn't support "#pragma once". ... Supported by some, but not all compilers. ... CPP supports two more ways of indicating that a header file should ...
    (comp.lang.c)
  • Re: C is too old? opinions?
    ... You insisted that this pragma is supported by many compilers, ... I cannot find any mention of it in the documentation for my current gcc ... If they support if, it is undocumented and probably deprecated. ... OS platforms and all the mainstream compilers used this pragma I know I ...
    (comp.lang.c)
  • Re: C is too old? opinions?
    ... You insisted that this pragma is supported by many compilers, ... I cannot find any mention of it in the documentation for my current gcc ... If they support if, it is undocumented and probably deprecated. ... OS platforms and all the mainstream compilers used this pragma I know I ...
    (comp.lang.c)
  • RE: pragma section
    ... The compilers available with eVC4.0 do not support that pragma. ... be using Windows CE 5.0 with Platform Builder or Visual Studio .NET 2005 ... > int j = 0; ...
    (microsoft.public.windowsce.app.development)