#INCLUDE skip at top of program

techElder

Well-known member
Are there examples of what would be in an include file that would require the kind of programming cautioned about in Manual 2, page 10? I don't recall reading info on how "the pre-processor output file" affects the compilation process.

I understand that the PICAXE include file is a simple substitution list.

PICAXE Manual 2, page 10:
As the compiler processes ’top-to-bottom’ on the pre-processor output file it is necessary to take care to ‘skip’ the #INCLUDE file (if necessary) when it is included at the top of a program.
Code:
Reset_here:  Goto Init        ; jump over include file

#INCLUDE “sphero.basinc”

Init:                         ; start program here
 

hippy

Technical Support
Staff member
Basically, if "sphero.basinc" happened to have an error routine at the top of the code in the form of ...

Code:
ErrorTrap:
  SerTxd( "Oops - We had an error!", CR, LF )
  Goto ErrorTrap
And there wasn't that "Goto Init" which skipped past the #INCLUDE; the program would always fall straight into that ErrorTrap routine rather than execute the code desired.

In practice, without that skip, program execution will fall into whatever is at the top of the included file and behaviour will likely be unpredictable, certainly not what was wanted.
 

techElder

Well-known member
Then this caution is really broader, in that it exists because the #INCLUDE is, in effect, compiled-in-sequence and needs to be placed with some evaluation of the main program.

The reason I ask is that I'm trying to find some general rule in placement of #INCLUDE files. I generally separate the content of included files by function in my programming, so they are included in the source after other directives and before initialization routines.

I'm thinking that I've seen other programming with all the include files at or near the top of the source, but I suppose the compiler there is expecting that placement.

I still don't have a general rule on placement, but perhaps there isn't one.
 

hippy

Technical Support
Staff member
I still don't have a general rule on placement, but perhaps there isn't one.
It's put them wherever you want them, but if you put them at the top of the code where the included code would end up being executed before the code you want executing first, you need to GOTO around the included code.

You can put the GOTO around the #INCLUDE in the top level .BAS program, or you can include a GOTO at the start of each .BASINC include program to a label at the end.

If there's no executable code in the #INCLUDE file, it is just SYMBOL or comments, then no GOTO is required.

TEST.BAS ...
Code:
Goto Main
#Include "INCLUDED.BASINC"
Main:
  SerTxd( "Got to main")
INCLUDED.BASINC ...
Code:
  Do
    SerTxd( "DON'T WANT TO START HERE")
  Loop
Or

TEST.BAS ...
Code:
#Include "INCLUDED.BASINC"
Main:
  SerTxd( "Got to main")
INCLUDED.BASINC ...
Code:
Goto Skip_Included

  Do
    SerTxd( "DON'T WANT TO START HERE")
  Loop

Skip_Included:
 
Top