$include: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 1 | + | The [[Compiler directive|compiler directive]] `{$include}` is used to: | |
| 8 | 2 | ||
| 9 | 3 | - read and parse a file, virtually inserting its contents at the location the directive was encountered, | |
| 10 | 4 | - insert compiler information, or | |
| 11 | 5 | - insert the value of an environment variable. | |
| 12 | 6 | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 7 | + | The `{$I}` directive, case-*sensitive*, is shorthand for the same directive. | |
| 20 | 8 | ||
| 21 | 9 | ## Sourcing files | |
| 22 | 10 | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 11 | + | Further 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}`. | |
| 36 | 12 | ||
| 37 | 13 | Files are searched for in the following order: | |
| 38 | 14 | ||
| 39 | 15 | 1. at the path specified; | |
| 40 | 16 | 2. in the directory containing the current source file; | |
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 17 | + | 3. in all include file paths. (`-Fi` or `-I` compiler parameters.) | |
| 53 | 18 | ||
| 54 | 19 | When specifying a file name, a file type hint suffix can be omitted. If you omit a suffix, the following suffixes are tried out: | |
| 55 | 20 | ||
| 56 | 21 | 1. *none*, meaning the exact name as specified (*without* any suffix) | |
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 22 | + | 2. `.inc` | |
| 23 | + | 3. `.pp` | |
| 24 | + | 4. `.pas` | |
| 70 | 25 | 5. (trailing dot stripped) | |
| 71 | 26 | ||
| 72 | 27 | This strategy applies to every directory in the search path as described above, and the first match is taken. | |
| 73 | 28 | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 29 | + | For 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. | |
| 129 | 30 | ||
| 130 | - | ||
| 31 | + | If 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”). | |
| 131 | 32 | ||
| 132 | 33 | Not finding a specified file constitutes a [[compile-time error|fatal error]]. | |
| 133 | 34 | ||
| 134 | 35 | ## Insertion of certain information | |
| 135 | 36 | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 37 | + | The 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%}`. | |
| 155 | 38 | ||
| 156 | - | ||
| 39 | + | This 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. | |
| 157 | 40 | ||
| 158 | 41 | The set of predefined variables are (case-insensitive): | |
| 159 | 42 | ||
| 160 | 43 | - location information | |
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 169 | - | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 173 | - | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 177 | - | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 44 | + | - `{$include %currentRoutine%}` expands to the current routine’s name. (available since FPC trunk revision 30873; FPC 3.2.0) | |
| 45 | + | - `{$include %file%}` expands to the file’s name the directive is found in. | |
| 46 | + | - `{$include %line%}` expands to the line number (starting at 1) where the directive is found at. | |
| 47 | + | - `{$include %lineNum%}` is the same as `{$I %line%}` but of an integer type. | |
| 190 | 48 | - [[Free Pascal supported targets|target]] information | |
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 195 | - | ||
| 196 | - | ||
| 197 | - | ||
| 198 | - | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - | ||
| 211 | - | ||
| 212 | - | ||
| 213 | - | ||
| 49 | + | - `{$include %FPCTargetCPU%}` expands to the target’s CPU name, e. g. `'x86_64'`. | |
| 50 | + | - `{$include %FPCTargetOS%}` expands to the target’s [[Operating System|OS]] name, e. g. [[Linux|'Linux']]. | |
| 214 | 51 | - compiler information | |
| 215 | - | ||
| 216 | - | ||
| 217 | - | ||
| 218 | - | ||
| 219 | - | ||
| 220 | - | ||
| 221 | - | ||
| 222 | - | ||
| 223 | - | ||
| 224 | - | ||
| 225 | - | ||
| 52 | + | - `{$include %FPCVersion%}` expands to the version number of the used [[FPC]], e. g. `'3.0.4'`. | |
| 226 | 53 | - regarding time/date of processing the directive | |
| 227 | - | ||
| 228 | - | ||
| 229 | - | ||
| 230 | - | ||
| 231 | - | ||
| 232 | - | ||
| 233 | - | ||
| 234 | - | ||
| 235 | - | ||
| 236 | - | ||
| 237 | - | ||
| 238 | - | ||
| 239 | - | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - | ||
| 243 | - | ||
| 244 | - | ||
| 245 | - | ||
| 246 | - | ||
| 247 | - | ||
| 248 | - | ||
| 249 | - | ||
| 250 | - | ||
| 251 | - | ||
| 252 | - | ||
| 253 | - | ||
| 254 | - | ||
| 255 | - | ||
| 256 | - | ||
| 257 | - | ||
| 258 | - | ||
| 259 | - | ||
| 260 | - | ||
| 261 | - | ||
| 262 | - | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - | ||
| 266 | - | ||
| 267 | - | ||
| 268 | - | ||
| 269 | - | ||
| 270 | - | ||
| 271 | - | ||
| 272 | - | ||
| 273 | - | ||
| 274 | - | ||
| 275 | - | ||
| 276 | - | ||
| 277 | - | ||
| 278 | - | ||
| 279 | - | ||
| 280 | - | ||
| 281 | - | ||
| 282 | - | ||
| 283 | - | ||
| 284 | - | ||
| 285 | - | ||
| 286 | - | ||
| 287 | - | ||
| 288 | - | ||
| 289 | - | ||
| 290 | - | ||
| 291 | - | ||
| 292 | - | ||
| 293 | - | ||
| 294 | - | ||
| 295 | - | ||
| 296 | - | ||
| 297 | - | ||
| 298 | - | ||
| 299 | - | ||
| 300 | - | ||
| 301 | - | ||
| 302 | - | ||
| 303 | - | ||
| 304 | - | ||
| 305 | - | ||
| 306 | - | ||
| 307 | - | ||
| 308 | - | ||
| 309 | - | ||
| 310 | - | ||
| 311 | - | ||
| 312 | - | ||
| 313 | - | ||
| 314 | - | ||
| 315 | - | ||
| 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) | |
| 60 | + | - `{$include %timeMinute%}` expands to the minute part of `{$I %time%}`. (Available since FPC trunk revision 38329) | |
| 61 | + | - `{$include %timeSecond%}` expands to the seconds part of `{$I %time%}`. (Available since FPC trunk revision 38329) | |
| 316 | 62 | ||
| 317 | - | ||
| 63 | + | Historical 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. | |
| 318 | 64 | ||
| 319 | 65 | ## See also | |
| 320 | 66 | ||
| 321 | 67 | - [§ “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* | |
| 322 | 68 | ||
| 323 | 69 | --- | |
| 324 | 70 | *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).* |
