It looks @inform7tips is no longer on twitter. I was able to retrieve these helpful threads from the Wayback Machine (original thread on compiling Inform to C, another wrinkle on C only sections), and I can’t find a whole lot of info on this topic, so I hope it’s okay if I take the liberty of reposting them here for posterity. (I’m not using the blockquote tag because it gives the regular text and code the same background color, which is harder to read, but, to be clear, everything below was written by @Zed.)
Compiling I7 v10 to C
I’ve mentioned that one can compile Inform7 v10 to C instead of Inform 6. Here’s how. (This one won’t be of much interest to people not using inform7 on the command-line.)
inform7 -format=C -internal "$INFORM7DIR/Internal" -transient /tmp -o project.c -source project.i7
If you omit -o project.c
it’ll automatically write a .c file with the basename of the source file to the same directory as the source file.
-transient
is specified because if you don’t have a -transient
or -external
specified, inform7 will create $HOME/Inform
if it doesn’t already exist so it has somewhere to create a Telemetry dir. The Project Index isn’t created if you use -source
.
Then, for gcc users:
gcc -I "$INFORM7DIR/Tangled" -lm project.c -o project
gcc needs to be able to find inform7_clib.h, -I
is one easy way to do it. The math library is needed, hence -lm
. If you omit -o project
, the executable ends up called a.out.
If you’re familiar with I7’s kits (née the I6 Template Layer) you know that one gets different code depending on whether you’re targeting Z-code or Glulx. Compiling to C means you get Glulx-flavored output with a word size of 4 (i.e., 32-bit); Z-code flavored is not an option. (But there will never be a Glulx interpreter involved, of course.)
I7’s own “Miniglk” is built in. According to the documentation, one could build it with one’s own choice of Glk implementation with some effort, but I haven’t tried that yet.
Instead of specifying the source file with -source
, one can use -project
, but when you use a project you can’t specify -o
. The output file will always be auto.inf in its usual location under project.inform/Build, despite being a C file. So…
inform7 -no-progress -no-census-update -format=C -internal $INFORM7DIR/Internal -transient /tmp -project project.inform
Since gcc normally uses the file suffix to determine the source format, you’ll need to add -x c
to tell it this is C.
gcc -I "$INFORM7DIR/Tangled" -lm -x c -o project project.inform/Build/auto.inf
And in either the source or project case one can specify an External directory with -external
if there are extensions there you want the compilation to have access to.)
Some references:
inform7 command-line usage
target virtual machines
C only sections
Know how you can put (for glulx only)
or (for Z-machine only)
at the end of a heading for that passage to be included only when compiling to that target? Well, here’s an undocumented feature: you can also say (not for glulx)
or (not for Z-machine)
.
Given just two choices, this would seem like a candidate for the most trivial factoid I’ve tweeted, but here’s another undocumented feature in v10: you can also say (for C only)
or (not for C)
.
I’ve described compiling to C as being “glulx-flavored”. One of the ramifications of that is that when compiling to C, (for glulx only) passages are included, and, of course (not for glulx) passages are excluded.
So how could you include something when compiling to I6 that would be compiled to glulx but exclude it when compiling to C? You could do, e.g.,
Chapter Foo (for glulx only)
Section Bar (not for C)
[code]
But, of course, these conditionals are more of interest when writing extensions than for an individual game. For your own game, you know what target you want and probably aren’t worried about maintaining the ability to switch among three different choices. So if you did want to have differences between C and ulx, unless you also wanted to be able to compile to the z-machine, you could get by with just (not for C)
and (not for glulx)
.