Sign in Join
Wiki copytemporary

$include: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The [[Compiler directive|compiler directive]] `{$include}` is used to:
22
33- read and parse a file, virtually inserting its contents at the location the directive was encountered,
44- insert compiler information, or
55- insert the value of an environment variable.
66
77The `{$I}` directive, case-*sensitive*, is shorthand for the same directive.
88
99## Sourcing files
1010
1111Further tokens can be read from a file using the `{$include}` directive to virtually substitute the file’s contents for the directive. A file to be substituted for the directive is specified like this `{$include magic.pas}`.
1212
1313Files are searched for in the following order:
1414
15151. at the path specified;
16162. in the directory containing the current source file;
17173. in all include file paths. (`-Fi` or `-I` compiler parameters.)
1818
1919When specifying a file name, a file type hint suffix can be omitted. If you omit a suffix, the following suffixes are tried out:
2020
21211. *none*, meaning the exact name as specified (*without* any suffix)
22222. `.inc`
23233. `.pp`
24244. `.pas`
25255. (trailing dot stripped)
2626
2727This strategy applies to every directory in the search path as described above, and the first match is taken.
2828
2929For Delphi compatibility, the special file name [[Asterisk|*]] will attempt to include a file of the current file’s base name. The suffix is supplemented as described above. You can, however, specify a (different) suffix too: `{$include *.dat}` will look for a file bearing same name as the source file, but the suffix (if applicable) will be replaced by (or augmented with) `.dat`. The suffix is the *last* component of a file name separated by a [[period|. (dot)]] (thus `{$I *.foo.bar.dat}` is effectively the same as `{$I *.dat}`). Note, if you are reading a file via a symbolic link, the supplied *unresolved* name is the source code file’s name, *not* the target file name.
3030
3131If a file name contains blank characters, the file name needs to be surrounded by [[&|' (typewriter quotes)]]. If a file name starts with a percent sign or asterisk, you must either use straight quotes as in the case of blanks, or specify an absolute or relative path like `{$I ./%wage.pas}` (on UNIX-like platforms `./` denotes the “current directory”).
3232
3333Not finding a specified file constitutes a [[compile-time error|fatal error]].
3434
3535## Insertion of certain information
3636
3737The compiler can be instructed to insert certain information at a specific spot. For that, a word is surrounded by [[Percent sign|%]] like in `{$include %internalVariable%}`.
3838
3939This directive always inserts [[String|strings]] (except `{$include %lineNum%}`). A set of predefined variables are consulted first. If there is no predefined variable of a given name, the value of the environment variable of the given name is tried to be retrieved. If this process fails, an empty string is inserted in order to provide a consistent behavior.
4040
4141The set of predefined variables are (case-insensitive):
4242
4343- location information
4444 - `{$include %currentRoutine%}` expands to the current routine’s name. (available since FPC trunk revision 30873; FPC 3.2.0)
4545 - `{$include %file%}` expands to the file’s name the directive is found in.
4646 - `{$include %line%}` expands to the line number (starting at 1) where the directive is found at.
4747 - `{$include %lineNum%}` is the same as `{$I %line%}` but of an integer type.
4848- [[Free Pascal supported targets|target]] information
4949 - `{$include %FPCTargetCPU%}` expands to the target’s CPU name, e. g. `'x86_64'`.
5050 - `{$include %FPCTargetOS%}` expands to the target’s [[Operating System|OS]] name, e. g. [[Linux|'Linux']].
5151- compiler information
5252 - `{$include %FPCVersion%}` expands to the version number of the used [[FPC]], e. g. `'3.0.4'`.
5353- regarding time/date of processing the directive
54- - `{$include %date%}` expands to a string of the form `'YYYY/mm/dd'`, e. g. <span class="s">`'//'`</span>.
55- - `{$include %dateYear%}` expands to <span class="s">`''`</span>. (Available since FPC trunk revision 38329)
56- - `{$include %dateMonth%}` expands to <span class="s">`''`</span>. (Available since FPC trunk revision 38329)
57- - `{$include %dateDay%}` expands to <span class="s">`''`</span>. (Available since FPC trunk revision 38329)
58- - `{$include %time%}` expands to a string of the form `'HH:MM:SS'`, e. g. <span class="s">`':42'`</span>.
59- - `{$include %timeHour%}` expands to <span class="s">`''`</span>. (Available since FPC trunk revision 38329)
54+ - `{$include %date%}` expands to a string of the form `'YYYY/mm/dd'`, e. g. `'//'`.
55+ - `{$include %dateYear%}` expands to `''`. (Available since FPC trunk revision 38329)
56+ - `{$include %dateMonth%}` expands to `''`. (Available since FPC trunk revision 38329)
57+ - `{$include %dateDay%}` expands to `''`. (Available since FPC trunk revision 38329)
58+ - `{$include %time%}` expands to a string of the form `'HH:MM:SS'`, e. g. `':42'`.
59+ - `{$include %timeHour%}` expands to `''`. (Available since FPC trunk revision 38329)
6060 - `{$include %timeMinute%}` expands to the minute part of `{$I %time%}`. (Available since FPC trunk revision 38329)
6161 - `{$include %timeSecond%}` expands to the seconds part of `{$I %time%}`. (Available since FPC trunk revision 38329)
6262
6363Historical remark: Previously *some* information like `{$include %FPCVersion%}` and `{$include %date%}` could be included with the long deprecated utility [`fpcsubst`](https://gitlab.com/freepascal.org/fpc/source/-/tree/3.3.1/utils/fpcsubst.pp), a macro preprocessor program.
6464
6565## See also
6666
6767- [§ “include file”](https://www.freepascal.org/docs-html/prog/progsu40.html) and [§ “include compiler info”](https://www.freepascal.org/docs-html/prog/progsu41.html) in the *Free Pascal programmer’s guide*
6868
6969---
7070*From the Free Pascal wiki, page "$include", as it stood on 2022-10-04 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history