Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Defines from the build

A switch is most useful when the build sets it, so one source makes several programs. The -d option defines a name before the first line of the file: tiko app.tiko -d EDITION=2 behaves as if the file began with #define EDITION 2. A #cmdline line puts such options in the source itself:

'' -d NAME=value defines a symbol before the first line of the file.
'' #cmdline puts compiler options in the source itself.
#cmdline "-d EDITION=2 -d SEATS=25"

'' A default, for a build that does not choose.
#ifndef EDITION
    #define EDITION 1
#endif

#if EDITION = 1
    print "standard edition"
#else
    print "professional edition"
#endif

#ifdef SEATS
    print "licensed for"; SEATS; " seats"
#endif

Output

professional edition
licensed for 25 seats
  • #cmdline "-d EDITION=2 -d SEATS=25" adds those options to the compile, as if they had been typed after tiko cmdline.tiko. When the compiler meets the line, it starts the compile again with them.
  • #ifndef EDITION gives the setting a default, so the program also builds when nothing chose a value.
  • The source wins. With the #cmdline line in place, tiko cmdline.tiko -d EDITION=1 still builds the professional edition. Take the line out and the same command builds the standard one; with no -d at all, the default applies and SEATS is not defined.

#cmdline counts only in the first source file named on the command line; in any other file it is warning TKO0334, "#cmdline is ignored outside the first source file". It is not limited to -d: any compiler option can go there, which is how the examples on this site carry flags such as -exx (see Building).

A module never sees the defines of the program that imports it, from -d or from #define: it is compiled once for every program that uses it. A setting for a module goes in its package's tiko.toml (see Packages).

Activities

  1. Remove the #cmdline line and build with -d EDITION=3.
  2. Add -d TRIAL=1 to the #cmdline line and print a notice under #ifdef TRIAL.