Re: "I want to ask about c++"
- From: James Kuyper <jameskuyper@xxxxxxxxxxx>
- Date: Thu, 18 Sep 2008 03:47:49 -0500 (CDT)
explorer wrote:
May you tell me the meaning of these line:
#ifndef _defs_h_
#include "defs.h"
#define _defs_h_
#endif
I know those called preprocessing, but do not know what they do.
If you want to ask about C++, ask in comp.lang.c++.moderated.
However, in this case, the answer is the same in both languages.
This code checks to see whether a macro named _defs_h_ has been defined. If no such macro has been defined, it looks for a file named defs.h, and processes the contents of that file just as if they were lines of code which replaced the #include directive; then it defines a macro named _defs_h_.
This is called a "header include guard", or simply a "header guard". This is generally done in a header file, to prevent defs.h from being accidentally #included twice. Not only is #including the file twice a waste of time, it can sometimes cause problems if defs.h defines anything. With very rare exceptions, you should not define an object in a header file, nor a non-static function. You can, however, define a static function, a macro, a struct type, a union type, an enumeration type, or a typedef, and in every one of those cases there's a problem if the definition is processed twice.
Key warning: Because _defs_h_ starts with an '_', it is an identifier which is reserved to the implementation. In C, it is "reserved for use as identifiers with file scope in both the ordinary and tag name spaces." In C++, it "is reserved to the implementation for use as a name in the global namespace". The reservations are restricted only because the second character of the identifier was a 'd'. If it had been an upper case letter or a second '_', the name would have been reserved to the implementation for ALL uses.
In general, you should always avoid using any name that start with an '_', so user code should never use a name like _defs_h_ for this purpose. It would be perfectly acceptable to use such a name in a standard header file, because standard headers are part of the implementation. However, the line
#include "defs.h"
should never appear in a standard header file, so this is probably defective code.
The example you gave is an external header guard. An internal header guard would appear INSIDE of defs.h:
#ifndef DEFS_H
#define DEFS_H
// The working part of the header goes here
#endif
The advantage of the internal header guard is that it only has to be written once, rather than having to appear in every location where #include "defs.h" appears. This makes the code easier to maintain. The disadvantage is that the compiler has to open defs.h, parse the entire thing, in order to find the terminating #endif, and then close it. Most people consider that to be a minor inefficiency compared to improved maintainability.
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- References:
- "I want to ask about c++"
- From: explorer
- "I want to ask about c++"
- Prev by Date: macro in c
- Next by Date: Re: "I want to ask about c++"
- Previous by thread: "I want to ask about c++"
- Next by thread: Re: "I want to ask about c++"
- Index(es):
Relevant Pages
|