Re: "I want to ask about c++"



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.
.



Relevant Pages

  • Re: header files including other files
    ... Really I guess I am asking advice about source code organisation. ... Now, in any particular module, the module header file declares exported ... compile it with no errors. ...
    (comp.arch.embedded)
  • Re: [DRIVER SUBMISSION] DRBD wants to go mainline
    ... header file. ... and then include some header after that. ... I want to be able to do a rate-limit per specific message/code fragment, ... When I earlier said I thought I was in macro hell, well, I was ...
    (Linux-Kernel)
  • Re: headerfiles and make?
    ... How can I compile using the above makefile when types.h is ... used in your source code tells the compiler to look for it. ... Since a Makefile isn't necessary for compiling a program the header ... rectlive tells the compiler to look for a header file and evaluate ...
    (comp.unix.programmer)
  • Re: About different versions of the same header files
    ... Now there are mulitple places where this header file is kept. ... I don't see why it would *need* to be in multiple locations. ... But the OP's problem is that he's dealing with a huge code base that's ...
    (comp.lang.c)
  • Re: Request for code comment
    ... William Pursell wrote: ... would like to have feedback about it. ... Usually a header advertises an interface to the outside ... You should always put a header guard, ...
    (comp.lang.c)