5.23 Ignoring Duplicate INCLUDE Operations


5.23 Ignoring Duplicate #INCLUDE Operations

As you begin to develop sophisticated modules and libraries, you eventually discover a big problem: Some header files will need to include other header files (e.g., the stdlib.hhf header file includes all the other HLA Standard Library header files). Well, this isn't actually a big problem, but a problem will occur when one header file includes another, and that second header file includes another, and that third header file includes another, and that last header file includes the first header file. Now this is a big problem.

There are two problems with a header file indirectly including itself. First, this creates an infinite loop in the compiler. The compiler will happily go on about its business including all these files over and over again until it runs out of memory or some other error occurs. Clearly this is not a good thing. The second problem that occurs (usually before the problem above) is that the second time HLA includes a header file, it starts complaining bitterly about duplicate symbol definitions. After all, the first time it reads the header file it processes all the declarations in that file, the second time around it views all those symbols as duplicate symbols.

HLA provides a special include directive that eliminates this problem: #includeonce. You use this directive exactly like you use the #include directive, e.g.,

 #includeonce( "myHeaderFile.hhf" ) 

If myHeaderFile.hhf directly or indirectly includes itself (with a #includeonce directive), then HLA will ignore the new request to include the file. Note, however, that if you use the #include directive, rather than #includeonce, HLA will include the file a second time. This was done in case you really do need to include a header file twice, for some.

The bottom line is this: You should always use the #includeonce directive to include header files you've created. In fact, you should get in the habit of always using #includeonce, even for header files created by others (the HLA Standard Library already has provisions to prevent recursive includes, so you don't have to worry about using #includeonce with the Standard Library header files).

There is another technique you can use to prevent recursive includes — using conditional compilation. A later chapter will discuss this option.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net