multiple include guards

Questions about the BASICtools and MakeItC
Post Reply
YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

multiple include guards

Post by YahooArchive »

About 6 months ago we switched to a Linux based pre-processor that was adapted
to accept BASIC.

This pre-processor warns about

no newline at end of file
Multiple include guards may be useful for

There does not appear to be a way to turn off those warnings, it doesn't accept
-Wall so for now its best to heed them.

So place an include guard around an included file (its good practice)

#ifndef THIS_FILE
#define THIS_FILE

... your source

#endif

And make sure the last line in the file is blank.



YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: multiple include guards

Post by YahooArchive »

> #ifndef THIS_FILE
> #define THIS_FILE

And (just to clairify) make sure that the "THIS_FILE" is different for each
include, if you are including a number of files, or only the first instance of
the file that has that same wrapper will be included.

I made this elementary mistake the other day, when I copied and pasted a
'template' and neglected to edit the #include wrapper...

Take care.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: multiple include guards

Post by YahooArchive »

I brought this issue up. It would be straightforward if the preprocessor was
consistent in issuing this warning. I have 8 include files in one of my
applications for the ARMExpress. The preprocessor consistently picked out only
one of them for the warning. I asked Bruce if he could tell me why that
particular file generated the warning. I've now put guards on that file and now
the warning has disappeared when compiling the main application. However running
a test program that only uses a different one of the same includes then
generates the warning. I suppose it is one of those mysteries in open source
files that it is not worth pursuing. I'll guard all my includes.

Regards to all.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: multiple include guards

Post by YahooArchive »

> I'll guard all my includes.

Hello Richard!

Yeah, it is my belief that it is a good habit to get into anyways. I do
know that it doesn't cost any code, and it surely does help to prevent an
erroneous event from transpiring - i.e. the redefinition of a common #def
used across multiple different libs (I was bit with that one too... ;-).

It will help too, if you are using global variables that are module-specific
(an oxy-moron, I know...) and defined therein vs. the main app by preventing
the compiler from trying to DIM them more than once.

These are just my opinions based on my own experiences - ymmv.

Take care.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: multiple include guards

Post by YahooArchive »

After sleeping on it I think I should have been more specific and not shown my
prejudice about open source. This behavior is clearly a bug in the preprocessor
modified for basic. Not a high priority to fix - but it would be nice.

I will guard all my includes. However I still think that this shouldn't really
be necessary unless you are developing general purpose libraries with nested
includes. I use includes in ARMBasic just to modularize multi thousand line code
and possibly simplify future reusability. After all if I call the include file
twice (unlikely because all the calls are together just preceding MAIN) the
compiler will immediately complain about duplicate declarations and the cause is
pretty obvious. I think I'd rather know about the underlying error rather than
having it hidden.

Take Care, Dick

Post Reply