1 <Chapter id="using-GHC">
6 <IndexTerm><Primary>GHC, using</Primary></IndexTerm>
7 <IndexTerm><Primary>using GHC</Primary></IndexTerm>
8 GHC is a command-line compiler: in order to compile a Haskell program,
9 GHC must be invoked on the source file(s) by typing a command to the
10 shell. The steps involved in compiling a program can be automated
11 using the <Command>make</Command> tool (this is especially useful if the program
12 consists of multiple source files which depend on each other). This
13 section describes how to use GHC from the command-line.
16 <Sect1 id="command-line-structure">
17 <Title>Overall command-line structure
21 <IndexTerm><Primary>structure, command-line</Primary></IndexTerm>
22 <IndexTerm><Primary>command-line structure</Primary></IndexTerm>
26 An invocation of GHC takes the following form:
38 Command-line arguments are either options or file names.
42 Command-line options begin with <Literal>-</Literal>. They may <Emphasis>not</Emphasis> be
43 grouped: <Option>-vO</Option> is different from <Option>-v -O</Option>. Options need not
44 precede filenames: e.g., <Command>ghc *.o -o foo</Command>. All options are
45 processed and then applied to all files; you cannot, for example, invoke
46 <Command>ghc -c -O1 Foo.hs -O2 Bar.hs</Command> to apply different optimisation
47 levels to the files <Filename>Foo.hs</Filename> and <Filename>Bar.hs</Filename>. For conflicting
48 options, e.g., <Option>-c -S</Option>, we reserve the right to do anything we
49 want. (Usually, the last one applies.)
54 <Sect1 id="file-suffixes">
55 <Title>Meaningful file suffixes
59 <IndexTerm><Primary>suffixes, file</Primary></IndexTerm>
60 <IndexTerm><Primary>file suffixes for GHC</Primary></IndexTerm>
64 File names with “meaningful” suffixes (e.g., <Filename>.lhs</Filename> or <Filename>.o</Filename>)
65 cause the “right thing” to happen to those files.
72 <Term><Filename>.lhs</Filename>:</Term>
75 <IndexTerm><Primary>lhs suffix</Primary></IndexTerm>
76 A “literate Haskell” module.
81 <Term><Filename>.hs</Filename>:</Term>
84 A not-so-literate Haskell module.
89 <Term><Filename>.hi</Filename>:</Term>
92 A Haskell interface file, probably compiler-generated.
97 <Term><Filename>.hc</Filename>:</Term>
100 Intermediate C file produced by the Haskell compiler.
105 <Term><Filename>.c</Filename>:</Term>
108 A C file not produced by the Haskell compiler.
113 <Term><Filename>.s</Filename>:</Term>
116 An assembly-language source file, usually
117 produced by the compiler.
122 <Term><Filename>.o</Filename>:</Term>
125 An object file, produced by an assembler.
133 Files with other suffixes (or without suffixes) are passed straight
139 <Sect1 id="options-help">
140 <Title>Help and verbosity options
144 <IndexTerm><Primary>help options (GHC)</Primary></IndexTerm>
145 <IndexTerm><Primary>verbose option (GHC)</Primary></IndexTerm>
149 A good option to start with is the <Option>-help</Option> (or <Option>-?</Option>) option.
150 <IndexTerm><Primary>-help option</Primary></IndexTerm>
151 <IndexTerm><Primary>-? option</Primary></IndexTerm>
152 GHC spews a long message to standard output and then exits.
156 The <Option>-v</Option><IndexTerm><Primary>-v option</Primary></IndexTerm> option makes GHC <Emphasis>verbose</Emphasis>: it
157 reports its version number and shows (on stderr) exactly how it invokes each
158 phase of the compilation system. Moreover, it passes
159 the <Option>-v</Option> flag to most phases; each reports
160 its version number (and possibly some other information).
164 Please, oh please, use the <Option>-v</Option> option when reporting bugs!
165 Knowing that you ran the right bits in the right order is always the
166 first thing we want to verify.
170 If you're just interested in the compiler version number, the
171 <Option>--version</Option><IndexTerm><Primary>--version option</Primary></IndexTerm> option prints out a
172 one-line string containing the requested info.
177 <Sect1 id="options-order">
178 <Title>Running the right phases in the right order
182 <IndexTerm><Primary>order of passes in GHC</Primary></IndexTerm>
183 <IndexTerm><Primary>pass ordering in GHC</Primary></IndexTerm>
184 The basic task of the <Command>ghc</Command> driver is to run each input file
185 through the right phases (compiling, linking, etc.).
189 The first phase to run is determined by the input-file suffix, and the
190 last phase is determined by a flag. If no relevant flag is present,
191 then go all the way through linking. This table summarises:
197 <ColSpec Align="Left">
198 <ColSpec Align="Left">
199 <ColSpec Align="Left">
200 <ColSpec Align="Left">
204 <Entry>Phase of the compilation system</Entry>
205 <Entry>Suffix saying “start here”</Entry>
206 <Entry>Flag saying “stop after”</Entry>
207 <Entry>(suffix of) output file</Entry>
212 literate pre-processor </Entry>
213 <Entry> .lhs </Entry>
219 C pre-processor (opt.) </Entry>
226 Haskell compiler </Entry>
228 <Entry> -C, -S </Entry>
229 <Entry> .hc, .s </Entry>
233 C compiler (opt.) </Entry>
234 <Entry> .hc or .c </Entry>
248 <Entry> other </Entry>
250 <Entry> a.out </Entry>
256 <IndexTerm><Primary>-C option</Primary></IndexTerm>
257 <IndexTerm><Primary>-S option</Primary></IndexTerm>
258 <IndexTerm><Primary>-c option</Primary></IndexTerm>
262 Thus, a common invocation would be: <Command>ghc -c Foo.hs</Command>
266 Note: What the Haskell compiler proper produces depends on whether a
267 native-code generator is used (producing assembly language) or not
272 The option <Option>-cpp</Option><IndexTerm><Primary>-cpp option</Primary></IndexTerm> must be given for the C
273 pre-processor phase to be run, that is, the pre-processor will be run
274 over your Haskell source file before continuing.
278 The option <Option>-E</Option><IndexTerm><Primary>-E
279 option</Primary></IndexTerm> runs just the pre-processing passes of
280 the compiler, dumping the result in a file. Note that this differs
281 from all GHCs prior to version 4.11, in which the result was dumped to the
282 standard output. If used in conjunction with -cpp, the output is the
283 code blocks of the original (literal) source after having put it
284 through the grinder that is the C pre-processor. Sans
285 <Option>-cpp</Option>, the output is the de-litted version of the
290 The option <Option>-optcpp-E</Option><IndexTerm><Primary>-optcpp-E option</Primary></IndexTerm> runs just the
291 pre-processing stage of the C-compiling phase, sending the result to
292 stdout. (For debugging or obfuscation contests, usually.)
297 <Sect1 id="options-output">
298 <Title>Re-directing the compilation output(s)
302 <IndexTerm><Primary>output-directing options</Primary></IndexTerm>
306 GHC's compiled output normally goes into a <Filename>.hc</Filename>, <Filename>.o</Filename>, etc., file,
307 depending on the last-run compilation phase. The option <Option>-o
308 foo</Option><IndexTerm><Primary>-o option</Primary></IndexTerm> re-directs the output of that last-run
309 phase to file <Filename>foo</Filename>.
313 Note: this “feature” can be counterintuitive:
314 <Command>ghc -C -o foo.o foo.hs</Command> will put the intermediate C code in the
315 file <Filename>foo.o</Filename>, name notwithstanding!
319 EXOTICA: But the <Option>-o</Option> option isn't of much use if you have
320 <Emphasis>several</Emphasis> input files… Non-interface output files are
321 normally put in the same directory as their corresponding input file
322 came from. You may specify that they be put in another directory
323 using the <Option>-odir <dir></Option><IndexTerm><Primary>-odir <dir> option</Primary></IndexTerm> (the
324 “Oh, dear” option). For example:
330 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
336 The output files, <Filename>Foo.o</Filename>, <Filename>Bar.o</Filename>, and <Filename>Bumble.o</Filename> would be
337 put into a subdirectory named after the architecture of the executing
338 machine (<Filename>sun4</Filename>, <Filename>mips</Filename>, etc). The directory must already
339 exist; it won't be created.
343 Note that the <Option>-odir</Option> option does <Emphasis>not</Emphasis> affect where the
344 interface files are put. In the above example, they would still be
345 put in <Filename>parse/Foo.hi</Filename>, <Filename>parse/Bar.hi</Filename>, and <Filename>gurgle/Bumble.hi</Filename>.
349 MORE EXOTICA: The <Option>-osuf <suffix></Option><IndexTerm><Primary>-osuf <suffix>
350 option</Primary></IndexTerm> will change the <Filename>.o</Filename> file suffix for object files to
351 whatever you specify. (We use this in compiling the prelude.).
352 Similarly, the <Option>-hisuf <suffix></Option><IndexTerm><Primary>-hisuf <suffix>
353 option</Primary></IndexTerm> will change the <Filename>.hi</Filename> file suffix for non-system
354 interface files (see <XRef LinkEnd="hi-options">).
358 The <Option>-hisuf</Option>/<Option>-osuf</Option> game is useful if you want to compile a program
359 with both GHC and HBC (say) in the same directory. Let HBC use the
360 standard <Filename>.hi</Filename>/<Filename>.o</Filename> suffixes; add <Option>-hisuf g_hi -osuf g_o</Option> to your
361 <Command>make</Command> rule for GHC compiling…
364 <sect2 id="keeping-intermediates">
365 <title>Keeping Intermediate Files</title>
366 <indexterm><primary>intermediate files, saving</primary>
368 <indexterm><primary><literal>.hc</literal> files, saving</primary>
370 <indexterm><primary><literal>.s</literal> files, saving</primary>
373 <para>The following options are useful for keeping certain
374 intermediate files around, when normally GHC would throw these
375 away after compilation:</para>
379 <term><literal>-keep-hc-files</literal></term>
381 <primary><literal>-keep-hc-files</literal></primary>
384 <para>Keep intermediate <literal>.hc</literal> files when
385 doing <literal>.hs</literal>-to-<literal>.o</literal>
386 compilations via C (NOTE: <literal>.hc</literal> files
387 aren't generated when using the native code generator, you
388 may need to use <literal>-fvia-C</literal> to force them
389 to be produced).</para>
394 <term><literal>-keep-s-files</literal></term>
396 <primary><literal>-keep-s-files</literal></primary>
399 <para>Keep intermediate <literal>.s</literal> files.</para>
404 <term><literal>-keep-raw-s-files</literal></term>
406 <primary><literal>-keep-raw-s-files</literal></primary>
409 <para>Keep intermediate <literal>.raw-s</literal> files.
410 These are the direct output from the C compiler, before
411 GHC does “assembly mangling” to produce the
412 <literal>.s</literal> file. Again, these are not produced
413 when using the native code generator.</para>
418 <term><literal>-keep-tmp-files</literal></term>
420 <primary><literal>-keep-tmp-files</literal></primary>
423 <primary>temporary files</primary>
424 <secondary>keeping</secondary>
427 <para>Instructs the GHC driver not to delete any of its
428 temporary files, which it normally keeps in
429 <literal>/tmp</literal> (or possibly elsewhere; see <xref
430 linkend="temp-files">). Running GHC with
431 <literal>-v</literal> will show you what temporary files
432 were generated along the way.</para>
438 <Sect2 id="saving-ghc-stderr">
439 <Title>Saving GHC's standard error output
443 <IndexTerm><Primary>standard error, saving</Primary></IndexTerm>
447 Sometimes, you may cause GHC to be rather chatty on standard error;
448 with <Option>-v</Option>, for example. You can instruct GHC to <Emphasis>append</Emphasis> this
449 output to a particular log file with a <Option>-odump <blah></Option><IndexTerm><Primary>-odump
450 <blah> option</Primary></IndexTerm> option.
455 <Sect2 id="temp-files">
456 <Title>Redirecting temporary files
460 <primary>temporary files</primary>
461 <secondary>redirecting</secondary>
465 If you have trouble because of running out of space in
466 <Filename>/tmp</Filename> (or wherever your installation thinks
467 temporary files should go), you may use the <Option>-tmpdir
468 <dir></Option><IndexTerm><Primary>-tmpdir <dir>
469 option</Primary></IndexTerm> option to specify an alternate directory.
470 For example, <Option>-tmpdir .</Option> says to put temporary files in
471 the current working directory.
475 Alternatively, use your <Constant>TMPDIR</Constant> environment
476 variable.<IndexTerm><Primary>TMPDIR environment
477 variable</Primary></IndexTerm> Set it to the name of the directory
478 where temporary files should be put. GCC and other programs will
479 honour the <Constant>TMPDIR</Constant> variable as well.
483 Even better idea: Set the <Constant>TMPDIR</Constant> variable when building GHC, and
484 never worry about <Constant>TMPDIR</Constant> again. (see the build documentation).
491 <Sect1 id="options-sanity">
492 <Title>Warnings and sanity-checking
496 <IndexTerm><Primary>sanity-checking options</Primary></IndexTerm>
497 <IndexTerm><Primary>warnings</Primary></IndexTerm>
498 GHC has a number of options that select which types of non-fatal error
499 messages, otherwise known as warnings, can be generated during
500 compilation. By default, you get a standard set of warnings which are
501 generally likely to indicate bugs in your program. These are:
502 <Option>-fwarn-overlpapping-patterns</Option>, <Option>-fwarn-duplicate-exports</Option>, and
503 <Option>-fwarn-missing-methods</Option>. The following flags are simple ways to
504 select standard “packages” of warnings:
511 <Term><Option>-Wnot</Option>:</Term>
514 <IndexTerm><Primary>-Wnot option</Primary></IndexTerm>
515 Turns off all warnings, including the standard ones.
521 <Term><Option>-w</Option>:</Term>
524 <IndexTerm><Primary>-w option</Primary></IndexTerm>
525 Synonym for <Option>-Wnot</Option>.
531 <Term><Option>-W</Option>:</Term>
534 <IndexTerm><Primary>-W option</Primary></IndexTerm>
535 Provides the standard warnings plus <Option>-fwarn-incomplete-patterns</Option>,
536 <Option>-fwarn-unused-imports</Option> and <Option>-fwarn-unused-binds</Option>.
542 <Term><Option>-Wall</Option>:</Term>
545 <IndexTerm><Primary>-Wall option</Primary></IndexTerm>
546 Turns on all warning options.
555 The full set of warning options is described below. To turn off any
556 warning, simply give the corresponding <Option>-fno-warn-...</Option> option on
564 <Term><Option>-fwarn-name-shadowing</Option>:</Term>
567 <IndexTerm><Primary>-fwarn-name-shadowing option</Primary></IndexTerm>
568 <IndexTerm><Primary>shadowing, warning</Primary></IndexTerm>This option causes a warning to be emitted whenever an inner-scope
569 value has the same name as an outer-scope value, i.e. the inner value
570 shadows the outer one. This can catch typographical errors that turn
571 into hard-to-find bugs, e.g., in the inadvertent cyclic definition
572 <Literal>let x = ... x ... in</Literal>.
576 Consequently, this option does <Emphasis>not</Emphasis> allow cyclic recursive
583 <Term><Option>-fwarn-overlapping-patterns</Option>:</Term>
586 <IndexTerm><Primary>-fwarn-overlapping-patterns option</Primary></IndexTerm>
587 <IndexTerm><Primary>overlapping patterns, warning</Primary></IndexTerm>
588 <IndexTerm><Primary>patterns, overlapping</Primary></IndexTerm>
589 By default, the compiler will warn you if a set of patterns are
595 f :: String -> Int
603 where the last pattern match in <Function>f</Function> won't ever be reached, as the
604 second pattern overlaps it. More often than not, redundant patterns
605 is a programmer mistake/error, so this option is enabled by default.
611 <Term><Option>-fwarn-incomplete-patterns</Option>:</Term>
614 <IndexTerm><Primary>-fwarn-incomplete-patterns option</Primary></IndexTerm>
615 <IndexTerm><Primary>incomplete patterns, warning</Primary></IndexTerm>
616 <IndexTerm><Primary>patterns, incomplete</Primary></IndexTerm>
617 Similarly for incomplete patterns, the function <Function>g</Function> below will fail
618 when applied to non-empty lists, so the compiler will emit a warning
619 about this when <Option>-fwarn-incomplete-patterns</Option> is enabled.
629 This option isn't enabled be default because it can be a bit noisy,
630 and it doesn't always indicate a bug in the program. However, it's
631 generally considered good practice to cover all the cases in your
638 <Term><Option>-fwarn-missing-methods</Option>:</Term>
641 <IndexTerm><Primary>-fwarn-missing-methods option</Primary></IndexTerm>
642 <IndexTerm><Primary>missing methods, warning</Primary></IndexTerm>
643 <IndexTerm><Primary>methods, missing</Primary></IndexTerm>
644 This option is on by default, and warns you whenever an instance
645 declaration is missing one or more methods, and the corresponding
646 class declaration has no default declaration for them.
652 <Term><Option>-fwarn-missing-fields</Option>:</Term>
655 <IndexTerm><Primary>-fwarn-missing-fields option</Primary></IndexTerm>
656 <IndexTerm><Primary>missing fields, warning</Primary></IndexTerm>
657 <IndexTerm><Primary>fields, missing</Primary></IndexTerm>
658 This option is on by default, and warns you whenever the construction
659 of a labelled field constructor isn't complete, missing initializers
660 for one or more fields. While not an error (the missing fields are
661 initialised with bottoms), it is often an indication of a programmer
668 <Term><Option>-fwarn-unused-imports</Option>:</Term>
671 <IndexTerm><Primary>-fwarn-unused-imports option</Primary></IndexTerm>
672 <IndexTerm><Primary>unused imports, warning</Primary></IndexTerm>
673 <IndexTerm><Primary>imports, unused</Primary></IndexTerm>
674 Report any objects that are explicitly imported but never used.
680 <Term><Option>-fwarn-unused-binds</Option>:</Term>
683 <IndexTerm><Primary>-fwarn-unused-binds option</Primary></IndexTerm>
684 <IndexTerm><Primary>unused binds, warning</Primary></IndexTerm>
685 <IndexTerm><Primary>binds, unused</Primary></IndexTerm>
686 Report any function definitions (and local bindings) which are unused.
687 For top-level functions, the warning is only given if the binding is
693 <Term><Option>-fwarn-unused-matches</Option>:</Term>
696 <IndexTerm><Primary>-fwarn-unused-matches option</Primary></IndexTerm>
697 <IndexTerm><Primary>unused matches, warning</Primary></IndexTerm>
698 <IndexTerm><Primary>matches, unused</Primary></IndexTerm>
699 Report all unused variables which arise from pattern matches,
700 including patterns consisting of a single variable. For instance <Literal>f x
701 y = []</Literal> would report <VarName>x</VarName> and <VarName>y</VarName> as unused. To eliminate the warning,
702 all unused variables can be replaced with wildcards.
708 <Term><Option>-fwarn-duplicate-exports</Option>:</Term>
711 <IndexTerm><Primary>-fwarn-duplicate-exports option</Primary></IndexTerm>
712 <IndexTerm><Primary>duplicate exports, warning</Primary></IndexTerm>
713 <IndexTerm><Primary>export lists, duplicates</Primary></IndexTerm>
714 Have the compiler warn about duplicate entries in export lists. This
715 is useful information if you maintain large export lists, and want to
716 avoid the continued export of a definition after you've deleted (one)
717 mention of it in the export list.
721 This option is on by default.
727 <Term><Option>-fwarn-type-defaults</Option>:</Term>
730 <IndexTerm><Primary>-fwarn-type-defaults option</Primary></IndexTerm>
731 <IndexTerm><Primary>defaulting mechanism, warning</Primary></IndexTerm>
732 Have the compiler warn/inform you where in your source the Haskell
733 defaulting mechanism for numeric types kicks in. This is useful
734 information when converting code from a context that assumed one
735 default into one with another, e.g., the `default default' for Haskell
736 1.4 caused the otherwise unconstrained value <Constant>1</Constant> to be given
737 the type <Literal>Int</Literal>, whereas Haskell 98 defaults it to
738 <Literal>Integer</Literal>. This may lead to differences in performance and
739 behaviour, hence the usefulness of being non-silent about this.
743 This warning is off by default.
749 <Term><Option>-fwarn-missing-signatures</Option>:</Term>
752 <IndexTerm><Primary>-fwarn-missing-signatures option</Primary></IndexTerm>
753 <IndexTerm><Primary>type signatures, missing</Primary></IndexTerm>
754 If you would like GHC to check that every top-level function/value has
755 a type signature, use the <Option>-fwarn-missing-signatures</Option> option. This
756 option is off by default.
764 If you're feeling really paranoid, the <Option>-dcore-lint</Option>
765 option<IndexTerm><Primary>-dcore-lint option</Primary></IndexTerm> is a good choice. It turns on
766 heavyweight intra-pass sanity-checking within GHC. (It checks GHC's
772 <Sect1 id="separate-compilation">
773 <Title>Separate compilation
777 <IndexTerm><Primary>separate compilation</Primary></IndexTerm>
778 <IndexTerm><Primary>recompilation checker</Primary></IndexTerm>
779 <IndexTerm><Primary>make and recompilation</Primary></IndexTerm>
780 This section describes how GHC supports separate compilation.
783 <Sect2 id="hi-files">
784 <Title>Interface files
788 <IndexTerm><Primary>interface files</Primary></IndexTerm>
789 <IndexTerm><Primary>.hi files</Primary></IndexTerm>
793 When GHC compiles a source file <Filename>F</Filename> which contains a module <Literal>A</Literal>, say,
794 it generates an object <Filename>F.o</Filename>, <Emphasis>and</Emphasis> a companion <Emphasis>interface
795 file</Emphasis> <Filename>A.hi</Filename>. The interface file is not intended for human
796 consumption, as you'll see if you take a look at one. It's merely
797 there to help the compiler compile other modules in the same program.
801 NOTE: Having the name of the interface file follow the module name and
802 not the file name, means that working with tools such as <Command>make</Command>
803 become harder. <Command>make</Command> implicitly assumes that any output files
804 produced by processing a translation unit will have file names that
805 can be derived from the file name of the translation unit. For
806 instance, pattern rules becomes unusable. For this reason, we
807 recommend you stick to using the same file name as the module name.
811 The interface file for <Literal>A</Literal> contains information needed by the compiler
812 when it compiles any module <Literal>B</Literal> that imports <Literal>A</Literal>, whether directly or
813 indirectly. When compiling <Literal>B</Literal>, GHC will read <Filename>A.hi</Filename> to find the
814 details that it needs to know about things defined in <Literal>A</Literal>.
818 Furthermore, when compiling module <Literal>C</Literal> which imports <Literal>B</Literal>, GHC may
819 decide that it needs to know something about <Literal>A</Literal>—for example, <Literal>B</Literal>
820 might export a function that involves a type defined in <Literal>A</Literal>. In this
821 case, GHC will go and read <Command>A.hi</Command> even though <Literal>C</Literal> does not explicitly
822 import <Literal>A</Literal> at all.
826 The interface file may contain all sorts of things that aren't
827 explicitly exported from <Literal>A</Literal> by the programmer. For example, even
828 though a data type is exported abstractly, <Filename>A.hi</Filename> will contain the
829 full data type definition. For small function definitions, <Filename>A.hi</Filename>
830 will contain the complete definition of the function. For bigger
831 functions, <Filename>A.hi</Filename> will contain strictness information about the
832 function. And so on. GHC puts much more information into <Filename>.hi</Filename> files
833 when optimisation is turned on with the <Option>-O</Option> flag. Without <Option>-O</Option> it
834 puts in just the minimum; with <Option>-O</Option> it lobs in a whole pile of stuff.
835 <IndexTerm><Primary>optimsation, effect on .hi files</Primary></IndexTerm>
839 <Filename>A.hi</Filename> should really be thought of as a compiler-readable version of
840 <Filename>A.o</Filename>. If you use a <Filename>.hi</Filename> file that wasn't generated by the same
841 compilation run that generates the <Filename>.o</Filename> file the compiler may assume
842 all sorts of incorrect things about <Literal>A</Literal>, resulting in core dumps and
843 other unpleasant happenings.
848 <Sect2 id="options-finding-imports">
849 <Title>Finding interface files
853 <IndexTerm><Primary>interface files, finding them</Primary></IndexTerm>
854 <IndexTerm><Primary>finding interface files</Primary></IndexTerm>
858 In your program, you import a module <Literal>Foo</Literal> by saying
859 <Literal>import Foo</Literal>. GHC goes looking for an interface file, <Filename>Foo.hi</Filename>.
860 It has a builtin list of directories (notably including <Filename>.</Filename>) where
868 <Term><Option>-i<dirs></Option></Term>
871 <IndexTerm><Primary>-i<dirs> option</Primary></IndexTerm>This flag
872 prepends a colon-separated list of <Filename>dirs</Filename> to the “import
873 directories” list.
874 See also <XRef LinkEnd="recomp"> for the significance of using
875 relative and absolute pathnames in the <Option>-i</Option> list.
881 <Term><Option>-i</Option></Term>
884 resets the “import directories” list back to nothing.
890 <Term><Option>-fno-implicit-prelude</Option></Term>
893 <IndexTerm><Primary>-fno-implicit-prelude option</Primary></IndexTerm>
894 GHC normally imports <Filename>Prelude.hi</Filename> files for you. If you'd rather it
895 didn't, then give it a <Option>-fno-implicit-prelude</Option> option.
896 The idea is that you can then import a Prelude of your own. (But don't call it <Literal>Prelude</Literal>;
897 the Haskell module namespace is flat, and you must not conflict with any Prelude module.)
900 Even though you have not imported the Prelude, all the built-in syntax still refers to
901 the built-in Haskell Prelude types and values, as specified by the Haskell Report.
902 For example, the type <Literal>[Int]</Literal>
903 still means <Literal>Prelude.[] Int</Literal>; tuples continue to refer to the standard Prelude
904 tuples; the translation for list comprehensions continues to use <Literal>Prelude.map</Literal> etc.
906 <Para> With one group of exceptions! You may want to define your own numeric class hierarchy.
907 It completely defeats that purpose if the literal "1" means "<Literal>Prelude.fromInteger 1</Literal>",
908 which is what the Haskell Report specifies. So the <Option>-fno-implicit-prelude</Option> flag causes
909 the following pieces of built-in syntax to refer to whatever is in scope, not the Prelude versions:
913 Integer and fractional literals mean "<Literal>fromInteger 1</Literal>" and "<Literal>fromRational 3.2</Literal>",
914 not the Prelude-qualified versions; both in expressions and in patterns.
919 Negation (e.g. "<Literal>- (f x)</Literal>") means "<Literal>negate (f x)</Literal>" (not <Literal>Prelude.negate</Literal>).
924 In an n+k pattern, the standard Prelude <Literal>Ord</Literal> class is used for comparison, but the
925 necessary subtraction uses
926 whatever "<Literal>(-)</Literal>" is in scope (not "<Literal>Prelude.(-)</Literal>").
935 <Term><Option>-I<dir></Option></Term>
938 <IndexTerm><Primary>-I<dir> option</Primary></IndexTerm>
939 Once a Haskell module has been compiled to C (<Filename>.hc</Filename> file), you may
940 wish to specify where GHC tells the C compiler to look for <Filename>.h</Filename> files.
941 (Or, if you are using the <Option>-cpp</Option> option<IndexTerm><Primary>-cpp option</Primary></IndexTerm>, where
942 it tells the C pre-processor to look…) For this purpose, use a <Option>-I</Option>
943 option in the usual C-ish way.
953 <Sect2 id="hi-options">
954 <Title>Other options related to interface files
958 <IndexTerm><Primary>interface files, options</Primary></IndexTerm>
959 The interface output may be directed to another file
960 <Filename>bar2/Wurble.iface</Filename> with the option <Option>-ohi bar2/Wurble.iface</Option><IndexTerm><Primary>-ohi
961 <file> option</Primary></IndexTerm> (not recommended).
965 To avoid generating an interface file at all, use a <Option>-nohi</Option>
966 option.<IndexTerm><Primary>-nohi option</Primary></IndexTerm>
970 The compiler does not overwrite an existing <Filename>.hi</Filename> interface file if
971 the new one is byte-for-byte the same as the old one; this is friendly
972 to <Command>make</Command>. When an interface does change, it is often enlightening to
973 be informed. The <Option>-hi-diffs</Option><IndexTerm><Primary>-hi-diffs option</Primary></IndexTerm> option will
974 make GHC run <Command>diff</Command> on the old and new <Filename>.hi</Filename> files. You can also
975 record the difference in the interface file itself, the
976 <Option>-keep-hi-diffs</Option><IndexTerm><Primary>-keep-hi-diffs</Primary></IndexTerm> option takes care of that.
980 The <Filename>.hi</Filename> files from GHC contain “usage” information which changes
981 often and uninterestingly. If you really want to see these changes
982 reported, you need to use the
983 <Option>-hi-diffs-with-usages</Option><IndexTerm><Primary>-hi-diffs-with-usages option</Primary></IndexTerm>
988 Interface files are normally jammed full of compiler-produced
989 <Emphasis>pragmas</Emphasis>, which record arities, strictness info, etc. If you
990 think these pragmas are messing you up (or you are doing some kind of
991 weird experiment), you can tell GHC to ignore them with the
992 <Option>-fignore-interface-pragmas</Option><IndexTerm><Primary>-fignore-interface-pragmas
993 option</Primary></IndexTerm> option.
997 When compiling without optimisations on, the compiler is extra-careful
998 about not slurping in data constructors and instance declarations that
999 it will not need. If you believe it is getting it wrong and not
1000 importing stuff which you think it should, this optimisation can be
1001 turned off with <Option>-fno-prune-tydecls</Option> and <Option>-fno-prune-instdecls</Option>.
1002 <IndexTerm><Primary>-fno-prune-tydecls option</Primary></IndexTerm><IndexTerm><Primary>-fno-prune-instdecls
1003 option</Primary></IndexTerm>
1007 See also <XRef LinkEnd="options-linker">, which describes how the linker finds standard
1014 <Title>The recompilation checker
1017 <IndexTerm><Primary>recompilation checker</Primary></IndexTerm>
1022 <Term><Option>-recomp</Option></Term>
1023 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
1026 (On by default) Turn on recompilation checking. This will stop
1027 compilation early, leaving an existing <filename>.o</filename> file in
1028 place, if it can be determined that the module does not need to be
1034 <Term><Option>-no-recomp</Option></Term>
1035 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
1038 Turn off recompilation checking.
1046 In the olden days, GHC compared the newly-generated
1047 <Filename>.hi</Filename> file with the previous version; if they were
1048 identical, it left the old one alone and didn't change its
1049 modification date. In consequence, importers of a module with an
1050 unchanged output <Filename>.hi</Filename> file were not recompiled.
1054 This doesn't work any more. In our earlier example, module
1055 <Literal>C</Literal> does not import module <Literal>A</Literal>
1056 directly, yet changes to <Filename>A.hi</Filename> should force a
1057 recompilation of <Literal>C</Literal>. And some changes to
1058 <Literal>A</Literal> (changing the definition of a function that
1059 appears in an inlining of a function exported by <Literal>B</Literal>,
1060 say) may conceivably not change <Filename>B.hi</Filename> one jot. So
1065 GHC keeps a version number on each interface file, and on each type
1066 signature within the interface file. It also keeps in every interface
1067 file a list of the version numbers of everything it used when it last
1068 compiled the file. If the source file's modification date is earlier
1069 than the <Filename>.o</Filename> file's date (i.e. the source hasn't
1070 changed since the file was last compiled), and the
1071 <option>-recomp</option> is given on the command line, GHC will be
1072 clever. It compares the version numbers on the things it needs this
1073 time with the version numbers on the things it needed last time
1074 (gleaned from the interface file of the module being compiled); if
1075 they are all the same it stops compiling rather early in the process
1076 saying “Compilation IS NOT required”. What a beautiful
1081 Patrick Sansom had a workshop paper about how all this is done (though
1082 the details have changed quite a bit). <ULink URL="mailto:sansom@dcs.gla.ac.uk">Ask him</ULink> if you want a copy.
1088 <Sect2 id="using-make">
1089 <Title>Using <Command>make</Command>
1093 <IndexTerm><Primary><literal>make</literal></Primary></IndexTerm>
1097 It is reasonably straightforward to set up a <Filename>Makefile</Filename> to use with GHC, assuming you name your source files the same as your modules.
1105 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
1107 SRCS = Main.lhs Foo.lhs Bar.lhs
1108 OBJS = Main.o Foo.o Bar.o
1110 .SUFFIXES : .o .hs .hi .lhs .hc .s
1114 $(HC) -o $@ $(HC_OPTS) $(OBJS)
1116 # Standard suffix rules
1121 $(HC) -c $< $(HC_OPTS)
1124 $(HC) -c $< $(HC_OPTS)
1126 # Inter-module dependencies
1127 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
1128 Main.o Main.hc Main.s : Foo.hi Baz.hi # Main imports Foo and Baz
1134 (Sophisticated <Command>make</Command> variants may achieve some of the above more
1135 elegantly. Notably, <Command>gmake</Command>'s pattern rules let you write the more
1143 $(HC) -c $< $(HC_OPTS)
1149 What we've shown should work with any <Command>make</Command>.)
1153 Note the cheesy <Literal>.o.hi</Literal> rule: It records the dependency of the
1154 interface (<Filename>.hi</Filename>) file on the source. The rule says a <Filename>.hi</Filename> file can
1155 be made from a <Filename>.o</Filename> file by doing…nothing. Which is true.
1159 Note the inter-module dependencies at the end of the Makefile, which
1166 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
1172 They tell <Command>make</Command> that if any of <Literal>Foo.o</Literal>, <Literal>Foo.hc</Literal> or <Literal>Foo.s</Literal> have an
1173 earlier modification date than <Literal>Baz.hi</Literal>, then the out-of-date file
1174 must be brought up to date. To bring it up to date, <Literal>make</Literal> looks for
1175 a rule to do so; one of the preceding suffix rules does the job
1181 <sect2 id="sec-makefile-dependencies">
1182 <title>Dependency generation</title>
1183 <indexterm><primary>dependencies in Makefiles</primary></indexterm>
1184 <indexterm><primary>Makefile dependencies</primary></indexterm>
1186 <para>Putting inter-dependencies of the form <Literal>Foo.o :
1187 Bar.hi</Literal> into your <Filename>Makefile</Filename> by hand
1188 is rather error-prone. Don't worry, GHC has support for
1189 automatically generating the required dependencies. Add the
1190 following to your <Filename>Makefile</Filename>:</para>
1194 ghc -M $(HC_OPTS) $(SRCS)
1197 <para>Now, before you start compiling, and any time you change
1198 the <Literal>imports</Literal> in your program, do <Command>make
1199 depend</Command> before you do <Command>make
1200 cool_pgm</Command>. <Command>ghc -M</Command> will append
1201 the needed dependencies to your
1202 <Filename>Makefile</Filename>.</Para>
1204 <para>In general, if module <Literal>A</Literal> contains the
1211 then <command>ghc -M</command> will generate a dependency
1218 If module <literal>A</literal> contains the line
1221 import {-# SOURCE #-} B ...blah...
1224 then <command>ghc -M</command> will generate a dependency
1231 (See <xref linkend="hi-files"> for details of interface files.)
1232 If <literal>A</literal> imports multiple modules, then there
1233 will be multiple lines with <filename>A.o</filename> as the
1236 <para>By default, <Command>ghc -M</Command> generates all the
1237 dependencies, and then concatenates them onto the end of
1238 <Filename>makefile</Filename> (or <Filename>Makefile</Filename>
1239 if <Filename>makefile</Filename> doesn't exist) bracketed by the
1240 lines "<Literal># DO NOT DELETE: Beginning of Haskell
1241 dependencies</Literal>" and "<Literal># DO NOT DELETE: End
1242 of Haskell dependencies</Literal>". If these lines already
1243 exist in the <Filename>makefile</Filename>, then the old
1244 dependencies are deleted first.</para>
1246 <para>Internally, GHC uses a script to generate the
1247 dependencies, called <command>mkdependHS</command>. This script
1248 has some options of its own, which you might find useful.
1249 Options can be passed directly to <command>mkdependHS</command>
1250 with GHC's <literal>-optdep</literal> option. For example, to
1251 generate the dependencies into a file called
1252 <literal>.depend</literal> instead of
1253 <literal>Makefile</literal>:</para>
1256 ghc -M -optdep-f optdep.depend ...
1259 <para>The full list of options accepted by
1260 <command>mkdependHS</command> is:</para>
1265 <term><option>-w</option></term>
1267 <para>Turn off warnings about interface file shadowing.</para>
1272 <term><option>-f blah</option></term>
1274 <para>Use <Filename>blah</Filename> as the makefile,
1275 rather than <Filename>makefile</Filename> or
1276 <Filename>Makefile</Filename>. If
1277 <Filename>blah</Filename> doesn't exist,
1278 <Command>mkdependHS</Command> creates it. We often use
1279 <Option>-f .depend</Option> to put the dependencies in
1280 <Filename>.depend</Filename> and then
1281 <Command>include</Command> the file
1282 <Filename>.depend</Filename> into
1283 <Filename>Makefile</Filename>.</para>
1288 <term><option>-o <osuf></option></term>
1290 <para>Use <Filename>.<osuf></Filename> as the
1291 "target file" suffix ( default: <Literal>o</Literal>).
1292 Multiple <Option>-o</Option> flags are permitted (GHC2.05
1293 onwards). Thus "<Option>-o hc -o o</Option>" will
1294 generate dependencies for <Filename>.hc</Filename> and
1295 <Filename>.o</Filename> files.</para>
1300 <term><option>-s <suf></option></term>
1302 <para>Make extra dependencies that declare that files with
1304 <Filename>.<suf>_<osuf></Filename>
1305 depend on interface files with suffix
1306 <Filename>.<suf>_hi</Filename>, or (for
1307 <Literal>{-# SOURCE #-}</Literal>
1308 imports) on <Filename>.hi-boot</Filename>. Multiple
1309 <Option>-s</Option> flags are permitted. For example,
1310 <Option>-o hc -s a -s b</Option> will make dependencies
1311 for <Filename>.hc</Filename> on <Filename>.hi</Filename>,
1312 <Filename>.a_hc</Filename> on
1313 <Filename>.a_hi</Filename>, and
1314 <Filename>.b_hc</Filename> on
1315 <Filename>.b_hi</Filename>. (Useful in conjunction
1316 with NoFib "ways".)</para>
1321 <term><option>--exclude-module=<file></option></term>
1323 <para>Regard <Filename><file></Filename> as
1324 "stable"; i.e., exclude it from having dependencies on
1330 <term><option>-x</option></term>
1332 <para>same as <option>--exclude-module</option></para>
1337 <term><option>--exclude-directory=<dirs></option></term>
1339 <para>Regard the colon-separated list of directories
1340 <Filename><dirs></Filename> as containing stable,
1341 don't generate any dependencies on modules therein.</para>
1346 <term><option>-xdirs</option></term>
1348 <para>same as <Option>--exclude-directory</Option>.</para>
1353 <term><option>--include-module=<file></option></term>
1355 <para>Regard <Filename><file></Filename> as not
1356 "stable"; i.e., generate dependencies on it (if any). This
1357 option is normally used in conjunction with the
1358 <Option>--exclude-directory</Option> option.</para>
1363 <term><option>--include-prelude</option></term>
1365 <para>Regard prelude libraries as unstable, i.e., generate
1366 dependencies on the prelude modules used (including
1367 <Literal>Prelude</Literal>). This option is normally only
1368 used by the various system libraries. If a
1369 <Option>-package</Option> option is used, dependencies will
1370 also be generated on the library's interfaces.</para>
1377 <Sect2 id="mutual-recursion">
1378 <Title>How to compile mutually recursive modules
1382 <IndexTerm><Primary>module system, recursion</Primary></IndexTerm>
1383 <IndexTerm><Primary>recursion, between modules</Primary></IndexTerm>
1387 Currently, the compiler does not have proper support for dealing with
1388 mutually recursive modules:
1398 newtype TA = MkTA Int
1416 When compiling either module A and B, the compiler will try (in vain)
1417 to look for the interface file of the other. So, to get mutually
1418 recursive modules off the ground, you need to hand write an interface
1419 file for A or B, so as to break the loop. These hand-written
1420 interface files are called <Literal>hi-boot</Literal> files, and are placed in a file
1421 called <Filename><module>.hi-boot</Filename>. To import from an <Literal>hi-boot</Literal> file instead
1422 of the standard <Filename>.hi</Filename> file, use the following syntax in the importing module:
1423 <IndexTerm><Primary>hi-boot files</Primary></IndexTerm>
1424 <IndexTerm><Primary>importing, hi-boot files</Primary></IndexTerm>
1430 import {-# SOURCE #-} A
1436 The hand-written interface need only contain the bare minimum of
1437 information needed to get the bootstrapping process started. For
1438 example, it doesn't need to contain declarations for <Emphasis>everything</Emphasis>
1439 that module <Literal>A</Literal> exports, only the things required by the module that
1440 imports <Literal>A</Literal> recursively.
1444 For the example at hand, the boot interface file for A would look like
1451 __interface A 1 404 where
1452 __export A TA{MkTA} ;
1453 1 newtype TA = MkTA PrelBase.Int ;
1459 The syntax is essentially the same as a normal <Filename>.hi</Filename> file
1460 (unfortunately), but you can usually tailor an existing <Filename>.hi</Filename> file to
1461 make a <Filename>.hi-boot</Filename> file.
1465 Notice that we only put the declaration for the newtype <Literal>TA</Literal> in the
1466 <Literal>hi-boot</Literal> file, not the signature for <Function>f</Function>, since <Function>f</Function> isn't used by
1467 <Literal>B</Literal>.
1471 The number “1” after “__interface A” gives the version number of module A;
1472 it is incremented whenever anything in A's interface file changes. The “404” is
1473 the version number of the interface file <Emphasis>syntax</Emphasis>; we change it when
1474 we change the syntax of interface files so that you get a better error message when
1475 you try to read an old-format file with a new-format compiler.
1479 The number “1” at the beginning of a declaration is the <Emphasis>version
1480 number</Emphasis> of that declaration: for the purposes of <Filename>.hi-boot</Filename> files
1481 these can all be set to 1. All names must be fully qualified with the
1482 <Emphasis>original</Emphasis> module that an object comes from: for example, the
1483 reference to <Literal>Int</Literal> in the interface for <Literal>A</Literal> comes from <Literal>PrelBase</Literal>,
1484 which is a module internal to GHC's prelude. It's a pain, but that's
1489 If you want an hi-boot file to export a data type, but you don't want to give its constructors
1490 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
1496 __interface A 1 404 where
1504 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
1508 <Emphasis>Note:</Emphasis> This is all a temporary solution, a version of the
1509 compiler that handles mutually recursive modules properly without the manual
1510 construction of interface files, is (allegedly) in the works.
1517 <sect1 id="packages">
1518 <title>Packages</title>
1519 <indexterm><primary>packages</primary></indexterm>
1521 <para>Packages are collections of libraries, conveniently grouped
1522 together as a single entity. The package system is flexible: a
1523 package may consist of Haskell code, foreign language code (eg. C
1524 libraries), or a mixture of the two. A package is a good way to
1525 group together related Haskell modules, and is essential if you
1526 intend to make the modules into a Windows DLL (see below).</para>
1528 <para>Because packages can contain both Haskell and C libraries, they
1529 are also a good way to provide convenient access to a Haskell
1530 layer over a C library.</para>
1532 <para>GHC comes with several packages (see <xref
1533 linkend="book-hslibs">), and packages can be added/removed from an
1534 existing GHC installation.</para>
1536 <sect2 id="listing-packages">
1537 <title>Listing the available packages</title>
1538 <indexterm><primary>packages</primary>
1539 <secondary>listing</secondary></indexterm>
1541 <para>To see what packages are currently installed, use the
1542 <literal>--list-packages</literal> option:</para>
1543 <indexterm><primary><literal>--list-packages</literal></primary>
1547 $ ghc --list-packages
1548 gmp, rts, std, lang, concurrent, data, net, posix, text, util
1551 <para>Note that your GHC installation might have a slightly
1552 different set of packages installed.</para>
1554 <para>The <literal>gmp</literal> and <literal>rts</literal>
1555 packages are always present, and represent the multi-precision
1556 integer and runtime system libraries respectively. The
1557 <literal>std</literal> package contains the Haskell prelude.
1558 The rest of the packages are optional libraries.</para>
1562 <sect2 id="using-packages">
1563 <title>Using a package</title>
1564 <indexterm><primary>packages</primary>
1565 <secondary>using</secondary></indexterm>
1567 <para>To use a package, add the <literal>-package</literal> flag
1568 to the command line:</para>
1572 <term><option>-package <lib></option></term>
1573 <indexterm><primary>-package <lib> option</primary></indexterm>
1575 <para>This option brings into scope all the modules from
1576 package <literal><lib></literal> (they still have to
1577 be imported in your Haskell source, however). It also
1578 causes the relevant libraries to be linked when linking is
1584 <para>Some packages depend on other packages, for example the
1585 <literal>text</literal> package makes use of some of the modules
1586 in the <literal>lang</literal> package. The package system
1587 takes care of all these dependencies, so that when you say
1588 <literal>-package text</literal> on the command line, you
1589 automatically get <literal>-package lang</literal> too.</para>
1592 <sect2 id="building-packages">
1593 <title>Building a package from Haskell source</title>
1594 <indexterm><primary>packages</primary>
1595 <secondary>building</secondary></indexterm>
1597 <para>It takes some special considerations to build a new
1602 <para>A package may contain several Haskell modules. A
1603 package may span many directories, or many packages may
1604 exist in a single directory. Packages may not be mutually
1609 <para>A package has a name
1610 (e.g. <filename>std</filename>)</para>
1614 <para>The Haskell code in a package may be built into one or
1615 more Unix libraries (e.g. <Filename>libHSfoo.a</Filename>),
1616 or a single DLL on Windows
1617 (e.g. <Filename>HSfoo.dll</Filename>). The restriction to a
1618 single DLL on Windows is that the package system is used to
1619 tell the compiler when it should make an inter-DLL call
1620 rather than an intra-DLL call (inter-DLL calls require an
1621 extra indirection).</para>
1625 <para>GHC does not maintain detailed cross-package
1626 dependency information. It does remember which modules in
1627 other packages the current module depends on, but not which
1628 things within those imported things.</para>
1632 <para>To compile a module which is to be part of a new package,
1633 use the <literal>-package-name</literal> option:</para>
1637 <term><option>-package-name <foo></option></term>
1638 <indexterm><primary><literal>-package-name</literal></primary>
1639 <secondary>option</secondary></indexterm>
1641 <para>This option is added to the command line when
1642 compiling a module that is destined to be part of package
1643 <literal>foo</literal>. If this flag is omitted then the
1644 default package <literal>Main</literal> is assumed.</para>
1649 <para>Failure to use the <literal>-package-name</literal> option
1650 when compiling a package will result in disaster on Windows, but
1651 is relatively harmless on Unix at the moment (it will just cause
1652 a few extra dependencies in some interface files). However,
1653 bear in mind that we might add support for Unix shared libraries
1654 at some point in the future.</para>
1656 <para>It is worth noting that on Windows, because each package
1657 is built as a DLL, and a reference to a DLL costs an extra
1658 indirection, intra-package references are cheaper than
1659 inter-package references. Of course, this applies to the
1660 <Filename>Main</Filename> package as well.</para>
1663 <sect2 id="package-management">
1664 <title>Package management</title>
1665 <indexterm><primary>packages</primary>
1666 <secondary>management</secondary></indexterm>
1668 <para>GHC uses a package configuration file, called
1669 <literal>packages.conf</literal>, which can be found in your GHC
1670 install directory. This file isn't intended to be edited
1671 directly, instead GHC provides options for adding & removing
1676 <term><option>--add-package</option></term>
1677 <indexterm><primary><literal>--add-package</literal></primary>
1678 <secondary>option</secondary></indexterm>
1680 <para>Reads a package specification (see below) on stdin,
1681 and adds it to the database of installed packages. The
1682 package specification must be a package that isn't already
1688 <term><option>--delete-package <foo></option></term>
1689 <indexterm><primary><literal>--delete-package</literal></primary>
1690 <secondary>option</secondary></indexterm>
1692 <para>Removes the specified package from the installed
1693 configuration.</para>
1698 <para>In both cases, the old package configuration file is saved
1699 in <literal>packages.conf.old</literal> in your GHC install
1700 directory, so in an emergency you can always copy this file into
1701 <literal>package.conf</literal> to restore the old
1704 <para>A package specification looks like this:</para>
1709 import_dirs = ["/usr/local/lib/imports/mypkg"],
1710 library_dirs = ["/usr/local/lib"],
1711 hs_libraries = ["HSmypkg" ],
1712 extra_libraries = ["HSmypkg_cbits"],
1714 c_includes = ["HsMyPkg.h"],
1715 package_deps = ["text", "data"],
1716 extra_ghc_opts = [],
1718 extra_ld_opts = ["-lmy_clib"]
1722 <para>Components of a package specification may be specified in
1723 any order, and are:</para>
1727 <term><literal>name</literal></term>
1728 <indexterm><primary><literal>name</literal></primary>
1729 <secondary>package specification</secondary></indexterm>
1731 <para>The package's name, for use with
1732 the <literal>-package</literal> flag and as listed in the
1733 <literal>--list-packages</literal> list.
1739 <term><literal>import_dirs</literal></term>
1740 <indexterm><primary><literal>import_dirs</literal></primary>
1741 <secondary>package specification</secondary></indexterm>
1743 <para>A list of directories containing interface files
1744 (<literal>.hi</literal> files) for this package.</para>
1749 <term><literal>library_dirs</literal></term>
1750 <indexterm><primary><literal>library_dirs</literal></primary>
1751 <secondary>package specification</secondary></indexterm>
1753 <para>A list of directories containing libraries for this
1759 <term><literal>hs_libraries</literal></term>
1760 <indexterm><primary><literal>hs_libraries</literal></primary>
1761 <secondary>package specification</secondary></indexterm>
1763 <para>A list of libraries containing Haskell code for this
1764 package, with the <literal>.a</literal> or
1765 <literal>.dll</literal> suffix omitted. On Unix, the
1766 <literal>lib</literal> prefix is also omitted.</para>
1771 <term><literal>extra_libraries</literal></term>
1772 <indexterm><primary><literal>extra_libraries</literal></primary>
1773 <secondary>package specification</secondary></indexterm>
1775 <para>A list of extra libraries for this package. The
1776 difference between <literal>hs_libraries</literal> and
1777 <literal>extra_libraries</literal> is that
1778 <literal>hs_libraries</literal> normally have several
1779 versions, to support profiling, parallel and other build
1780 options. The various versions are given different
1781 suffixes to distinguish them, for example the profiling
1782 version of the standard prelude library is named
1783 <filename>libHSstd_p.a</filename>, with the
1784 <literal>_p</literal> indicating that this is a profiling
1785 version. The suffix is added automatically by GHC for
1786 <literal>hs_libraries</literal> only, no suffix is added
1788 <literal>extra_libraries</literal>.</para>
1790 <para>Also, <literal>extra_libraries</literal> are placed
1791 on the linker command line before the
1792 <literal>hs_libraries</literal> for the same package. If
1793 your package has dependencies in the other direction, you
1794 might need to make two separate packages.</para>
1799 <term><literal>include_dirs</literal></term>
1800 <indexterm><primary><literal>include_dirs</literal></primary>
1801 <secondary>package specification</secondary></indexterm>
1803 <para>A list of directories containing C includes for this
1804 package (maybe the empty list).</para>
1809 <term><literal>c_includes</literal></term>
1810 <indexterm><primary><literal>c_includes</literal></primary>
1811 <secondary>package specification</secondary></indexterm>
1813 <para>A list of files to include for via-C compilations
1814 using this package. Typically this include file will
1815 contain function prototypes for any C functions used in
1816 the package, in case they end up being called as a result
1817 of Haskell functions from the package being
1823 <term><literal>package_deps</literal></term>
1824 <indexterm><primary><literal>package_deps</literal></primary>
1825 <secondary>package specification</secondary></indexterm>
1827 <para>A list of packages which this package depends
1833 <term><literal>extra_ghc_opts</literal></term>
1834 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
1835 <secondary>package specification</secondary></indexterm>
1837 <para>Extra arguments to be added to the GHC command line
1838 when this package is being used.</para>
1843 <term><literal>extra_cc_opts</literal></term>
1844 <indexterm><primary><literal>extra_cc_opts</literal></primary>
1845 <secondary>package specification</secondary></indexterm>
1847 <para>Extra arguments to be added to the gcc command line
1848 when this package is being used (only for via-C
1849 compilations).</para>
1854 <term><literal>extra_ld_opts</literal></term>
1855 <indexterm><primary><literal>extra_ld_opts</literal></primary>
1856 <secondary>package specification</secondary></indexterm>
1858 <para>Extra arguments to be added to the gcc command line
1859 (for linking) when this package is being used.</para>
1864 <para>For examples of more package specifications, take a look
1865 at the <literal>package.conf</literal> in your GHC
1866 installation.</para>
1871 <Sect1 id="options-optimise">
1872 <Title>Optimisation (code improvement)
1876 <IndexTerm><Primary>optimisation (GHC)</Primary></IndexTerm>
1877 <IndexTerm><Primary>improvement, code (GHC)</Primary></IndexTerm>
1881 The <Option>-O*</Option> options specify convenient “packages” of optimisation
1882 flags; the <Option>-f*</Option> options described later on specify
1883 <Emphasis>individual</Emphasis> optimisations to be turned on/off; the <Option>-m*</Option>
1884 options specify <Emphasis>machine-specific</Emphasis> optimisations to be turned
1888 <Sect2 id="optimise-pkgs">
1889 <Title><Option>-O*</Option>: convenient “packages” of optimisation flags.
1893 <IndexTerm><Primary>-O options</Primary></IndexTerm>
1897 There are <Emphasis>many</Emphasis> options that affect the quality of code
1898 produced by GHC. Most people only have a general goal, something like
1899 “Compile quickly” or “Make my program run like greased lightning.”
1900 The following “packages” of optimisations (or lack thereof) should
1905 Once you choose a <Option>-O*</Option> “package,” stick with it—don't chop and
1906 change. Modules' interfaces <Emphasis>will</Emphasis> change with a shift to a new
1907 <Option>-O*</Option> option, and you may have to recompile a large chunk of all
1908 importing modules before your program can again be run
1909 safely (see <XRef LinkEnd="recomp">).
1916 <Term>No <Option>-O*</Option>-type option specified:</Term>
1917 <IndexTerm><Primary>-O* not specified</Primary></IndexTerm>
1920 This is taken to mean: “Please compile quickly; I'm not over-bothered
1921 about compiled-code quality.” So, for example: <Command>ghc -c Foo.hs</Command>
1926 <Term><Option>-O</Option> or <Option>-O1</Option>:</Term>
1927 <IndexTerm><Primary>-O option</Primary></IndexTerm>
1928 <IndexTerm><Primary>-O1 option</Primary></IndexTerm>
1929 <IndexTerm><Primary>optimise</Primary><secondary>normally</secondary></IndexTerm>
1932 Means: “Generate good-quality code without taking too long about
1933 it.” Thus, for example: <Command>ghc -c -O Main.lhs</Command>
1938 <Term><Option>-O2</Option>:</Term>
1939 <IndexTerm><Primary>-O2 option</Primary></IndexTerm>
1940 <IndexTerm><Primary>optimise</Primary><secondary>aggressively</secondary></IndexTerm>
1943 Means: “Apply every non-dangerous optimisation, even if it means
1944 significantly longer compile times.”
1948 The avoided “dangerous” optimisations are those that can make
1949 runtime or space <Emphasis>worse</Emphasis> if you're unlucky. They are
1950 normally turned on or off individually.
1954 At the moment, <Option>-O2</Option> is <Emphasis>unlikely</Emphasis> to produce
1955 better code than <Option>-O</Option>.
1960 <Term><Option>-O2-for-C</Option>:</Term>
1961 <IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm>
1962 <IndexTerm><Primary>gcc, invoking with -O2</Primary></IndexTerm>
1965 Says to run GCC with <Option>-O2</Option>, which may be worth a few percent in
1966 execution speed. Don't forget <Option>-fvia-C</Option>, lest you use the native-code
1967 generator and bypass GCC altogether!
1972 <Term><Option>-Onot</Option>:</Term>
1973 <IndexTerm><Primary>-Onot option</Primary></IndexTerm>
1974 <IndexTerm><Primary>optimising, reset</Primary></IndexTerm>
1977 This option will make GHC “forget” any
1978 <Option>-O</Option>ish options it has seen so far. Sometimes useful;
1979 for example: <Command>make all
1980 EXTRA_HC_OPTS=-Onot</Command>.
1985 <Term><Option>-Ofile <file></Option>:</Term>
1986 <IndexTerm><Primary>-Ofile <file> option</Primary></IndexTerm>
1987 <IndexTerm><Primary>optimising, customised</Primary></IndexTerm>
1990 For those who need <Emphasis>absolute</Emphasis> control over
1991 <Emphasis>exactly</Emphasis> what options are used (e.g., compiler
1992 writers, sometimes :-), a list of options can be put in a file and
1993 then slurped in with <Option>-Ofile</Option>.
1997 In that file, comments are of the
1998 <Literal>#</Literal>-to-end-of-line variety; blank lines and most
1999 whitespace is ignored.
2003 Please ask if you are baffled and would like an example of <Option>-Ofile</Option>!
2011 At Glasgow, we don't use a <Option>-O*</Option> flag for day-to-day work. We use
2012 <Option>-O</Option> to get respectable speed; e.g., when we want to measure
2013 something. When we want to go for broke, we tend to use <Option>-O -fvia-C
2014 -O2-for-C</Option> (and we go for lots of coffee breaks).
2018 The easiest way to see what <Option>-O</Option> (etc.) “really mean” is to run with
2019 <Option>-v</Option>, then stand back in amazement. Alternatively, just look at the
2020 <Literal>HsC_minus<blah></Literal> lists in the GHC driver script.
2026 <Title><Option>-f*</Option>: platform-independent flags</Title>
2029 <IndexTerm><Primary>-f* options (GHC)</Primary></IndexTerm>
2030 <IndexTerm><Primary>-fno-* options (GHC)</Primary></IndexTerm>
2034 Flags can be turned <Emphasis>off</Emphasis> individually. (NB: I hope you have a
2035 good reason for doing this…) To turn off the <Option>-ffoo</Option> flag, just use
2036 the <Option>-fno-foo</Option> flag.<IndexTerm><Primary>-fno-<opt> anti-option</Primary></IndexTerm> So, for
2037 example, you can say <Option>-O2 -fno-strictness</Option>, which will then drop out
2038 any running of the strictness analyser.
2042 The options you are most likely to want to turn off are:
2048 <Option>-fno-strictness</Option><IndexTerm><Primary>-fno-strictness option</Primary></IndexTerm> (strictness
2049 analyser, because it is sometimes slow),
2055 <Option>-fno-specialise</Option><IndexTerm><Primary>-fno-specialise option</Primary></IndexTerm> (automatic
2056 specialisation of overloaded functions, because it can make your code
2057 bigger) (US spelling also accepted), and
2063 <Option>-fno-cpr-analyse</Option><IndexTerm><Primary>-fno-cpr-analyse option</Primary></IndexTerm> switches off the CPR (constructed product
2073 Should you wish to turn individual flags <Emphasis>on</Emphasis>, you are advised
2074 to use the <Option>-Ofile</Option> option, described above. Because the order in
2075 which optimisation passes are run is sometimes crucial, it's quite
2076 hard to do with command-line options.
2080 Here are some “dangerous” optimisations you <Emphasis>might</Emphasis> want to try:
2084 <Term><Option>-fvia-C</Option>:</Term>
2087 <IndexTerm><Primary>-fvia-C option</Primary></IndexTerm>
2088 <IndexTerm><Primary>native code generator, turning off</Primary></IndexTerm>
2092 Compile via C, and don't use the native-code generator. (There are many
2093 cases when GHC does this on its own.) You might pick up a little bit of
2094 speed by compiling via C (e.g. for floating-point intensive code on Intel).
2095 If you use <Function>_casm_</Function>s (which are utterly
2096 deprecated), you probably <Emphasis>have</Emphasis> to use
2097 <Option>-fvia-C</Option>.
2101 The lower-case incantation, <Option>-fvia-c</Option>, is synonymous.
2105 Compiling via C will probably be slower (in compilation time) than
2106 using GHC's native code generator.
2111 <Term><Option>-funfolding-interface-threshold<n></Option>:</Term>
2114 <IndexTerm><Primary>-funfolding-interface-threshold option</Primary></IndexTerm>
2115 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
2116 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
2117 (Default: 30) By raising or lowering this number, you can raise or
2118 lower the amount of pragmatic junk that gets spewed into interface
2119 files. (An unfolding has a “size” that reflects the cost in terms
2120 of “code bloat” of expanding that unfolding in another module. A
2121 bigger function would be assigned a bigger cost.)
2126 <Term><Option>-funfolding-creation-threshold<n></Option>:</Term>
2129 <IndexTerm><Primary>-funfolding-creation-threshold option</Primary></IndexTerm>
2130 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
2131 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
2132 (Default: 30) This option is similar to
2133 <Option>-funfolding-interface-threshold</Option>, except that it governs unfoldings
2134 within a single module. Increasing this figure is more likely to
2135 result in longer compile times than faster code. The next option is
2141 <Term><Option>-funfolding-use-threshold<n></Option>:</Term>
2144 <IndexTerm><Primary>-funfolding-use-threshold option</Primary></IndexTerm>
2145 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
2146 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
2147 (Default: 8) This is the magic cut-off figure for unfolding: below
2148 this size, a function definition will be unfolded at the call-site,
2149 any bigger and it won't. The size computed for a function depends on
2150 two things: the actual size of the expression minus any discounts that
2151 apply (see <Option>-funfolding-con-discount</Option>).
2157 <Term><Option>-funfolding-update-in-place<n></Option>:</Term>
2160 Switches on an experimental "optimisation". Switching it on makes the compiler
2161 a little keener to inline a function that returns a constructor, if the context is
2166 If we inlined plusInt we might get an opportunity to use update-in-place for
2173 <Term><Option>-funbox-strict-fields</Option>:</Term>
2176 <IndexTerm><Primary>-funbox-strict-fields option</Primary></IndexTerm>
2177 <IndexTerm><Primary>strict constructor fields</Primary></IndexTerm>
2178 <IndexTerm><Primary>constructor fields, strict</Primary></IndexTerm>
2182 This option causes all constructor fields which are marked strict
2183 (i.e. “!”) to be unboxed or unpacked if possible. For example:
2189 data T = T !Float !Float
2195 will create a constructor <Literal>T</Literal> containing two unboxed floats if the
2196 <Option>-funbox-strict-fields</Option> flag is given. This may not always be an
2197 optimisation: if the <Function>T</Function> constructor is scrutinised and the floats
2198 passed to a non-strict function for example, they will have to be
2199 reboxed (this is done automatically by the compiler).
2203 This option should only be used in conjunction with <Option>-O</Option>, in order to
2204 expose unfoldings to the compiler so the reboxing can be removed as
2205 often as possible. For example:
2212 f (T f1 f2) = f1 + f2
2218 The compiler will avoid reboxing <Function>f1</Function> and <Function>f2</Function> by inlining <Function>+</Function> on
2219 floats, but only when <Option>-O</Option> is on.
2223 Any single-constructor data is eligible for unpacking; for example
2229 data T = T !(Int,Int)
2235 will store the two <Literal>Int</Literal>s directly in the <Function>T</Function> constructor, by flattening
2236 the pair. Multi-level unpacking is also supported:
2243 data S = S !Int !Int
2249 will store two unboxed <Literal>Int#</Literal>s directly in the <Function>T</Function> constructor.
2254 <Term><Option>-fsemi-tagging</Option>:</Term>
2257 This option (which <Emphasis>does not work</Emphasis> with the native-code generator)
2258 tells the compiler to add extra code to test for already-evaluated
2259 values. You win if you have lots of such values during a run of your
2260 program, you lose otherwise. (And you pay in extra code space.)
2264 We have not played with <Option>-fsemi-tagging</Option> enough to recommend it.
2265 (For all we know, it doesn't even work anymore… Sigh.)
2270 <Term><Option>-fexcess-precision</Option>:</Term>
2273 When this option is given, intermediate floating point values can have
2274 a <Emphasis>greater</Emphasis> precision/range than the final type.
2275 Generally this is a good thing, but some programs may rely on the
2276 exact precision/range of <Literal>Float</Literal>/<Literal>Double</Literal>
2277 values and should not use this option for their compilation.
2287 <Title><Option>-m*</Option>: platform-specific flags</Title>
2290 <IndexTerm><Primary>-m* options (GHC)</Primary></IndexTerm>
2291 <IndexTerm><Primary>platform-specific options</Primary></IndexTerm>
2292 <IndexTerm><Primary>machine-specific options</Primary></IndexTerm>
2296 Some flags only make sense for particular target platforms.
2303 <Term><Option>-mv8</Option>:</Term>
2306 (SPARC machines)<IndexTerm><Primary>-mv8 option (SPARC only)</Primary></IndexTerm>
2307 Means to pass the like-named option to GCC; it says to use the
2308 Version 8 SPARC instructions, notably integer multiply and divide.
2309 The similiar <Option>-m*</Option> GCC options for SPARC also work, actually.
2314 <Term><Option>-mlong-calls</Option>:</Term>
2317 (HPPA machines)<IndexTerm><Primary>-mlong-calls option (HPPA only)</Primary></IndexTerm>
2318 Means to pass the like-named option to GCC. Required for Very Big
2319 modules, maybe. (Probably means you're in trouble…)
2324 <Term><Option>-monly-[32]-regs</Option>:</Term>
2327 (iX86 machines)<IndexTerm><Primary>-monly-N-regs option (iX86 only)</Primary></IndexTerm>
2328 GHC tries to “steal” four registers from GCC, for performance
2329 reasons; it almost always works. However, when GCC is compiling some
2330 modules with four stolen registers, it will crash, probably saying:
2333 Foo.hc:533: fixed or forbidden register was spilled.
2334 This may be due to a compiler bug or to impossible asm
2335 statements or clauses.
2338 Just give some registers back with <Option>-monly-N-regs</Option>. Try `3' first,
2339 then `2'. If `2' doesn't work, please report the bug to us.
2348 <Sect2 id="optimise-C-compiler">
2349 <Title>Code improvement by the C compiler.
2353 <IndexTerm><Primary>optimisation by GCC</Primary></IndexTerm>
2354 <IndexTerm><Primary>GCC optimisation</Primary></IndexTerm>
2358 The C compiler (GCC) is run with <Option>-O</Option> turned on. (It has
2363 If you want to run GCC with <Option>-O2</Option>—which may be worth a few
2364 percent in execution speed—you can give a
2365 <Option>-O2-for-C</Option><IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm> option.
2372 <Sect1 id="options-phases">
2373 <Title>Options related to a particular phase
2376 <Sect2 id="c-pre-processor">
2377 <Title>The C pre-processor
2381 <IndexTerm><Primary>pre-processing: cpp</Primary></IndexTerm>
2382 <IndexTerm><Primary>C pre-processor options</Primary></IndexTerm>
2383 <IndexTerm><Primary>cpp, pre-processing with</Primary></IndexTerm>
2387 The C pre-processor <Command>cpp</Command> is run over your Haskell code only if the
2388 <Option>-cpp</Option> option <IndexTerm><Primary>-cpp option</Primary></IndexTerm> is given. Unless you are
2389 building a large system with significant doses of conditional
2390 compilation, you really shouldn't need it.
2394 <Term><Option>-D<foo></Option>:</Term>
2397 <IndexTerm><Primary>-D<name> option</Primary></IndexTerm>
2398 Define macro <Constant><foo></Constant> in the usual way. NB: does <Emphasis>not</Emphasis> affect
2399 <Option>-D</Option> macros passed to the C compiler when compiling via C! For those,
2400 use the <Option>-optc-Dfoo</Option> hack… (see <XRef LinkEnd="forcing-options-through">).
2405 <Term><Option>-U<foo></Option>:</Term>
2408 <IndexTerm><Primary>-U<name> option</Primary></IndexTerm>
2409 Undefine macro <Command><foo></Command> in the usual way.
2414 <Term><Option>-I<dir></Option>:</Term>
2417 <IndexTerm><Primary>-I<dir> option</Primary></IndexTerm>
2418 Specify a directory in which to look for <Literal>#include</Literal> files, in
2427 The GHC driver pre-defines several macros when processing Haskell
2428 source code (<Filename>.hs</Filename> or <Filename>.lhs</Filename> files):
2435 <Term><Constant>__HASKELL98__</Constant>:</Term>
2438 <IndexTerm><Primary>__HASKELL98__</Primary></IndexTerm>
2439 If defined, this means that GHC supports the language defined by the
2445 <Term><Constant>__HASKELL__=98</Constant>:</Term>
2448 <IndexTerm><Primary>__HASKELL__</Primary></IndexTerm>
2449 In GHC 4.04 and later, the <Constant>__HASKELL__</Constant> macro is defined as having
2450 the value <Constant>98</Constant>.
2455 <Term><Constant>__HASKELL1__</Constant>:</Term>
2458 <IndexTerm><Primary>__HASKELL1__ macro</Primary></IndexTerm>
2459 If defined to <Emphasis>n</Emphasis>, that means GHC supports the Haskell language
2460 defined in the Haskell report version <Emphasis>1.n</Emphasis>. Currently 5. This
2461 macro is deprecated, and will probably disappear in future versions.
2466 <Term><Constant>__GLASGOW_HASKELL__</Constant>:</Term>
2469 <IndexTerm><Primary>__GLASGOW_HASKELL__ macro</Primary></IndexTerm>
2470 For version <Emphasis>n</Emphasis> of the GHC system, this will be <Literal>#define</Literal>d to
2471 <Emphasis>100n</Emphasis>. So, for version 4.00, it is 400.
2475 With any luck, <Constant>__GLASGOW_HASKELL__</Constant> will be undefined in all other
2476 implementations that support C-style pre-processing.
2480 (For reference: the comparable symbols for other systems are:
2481 <Constant>__HUGS__</Constant> for Hugs and <Constant>__HBC__</Constant> for Chalmers.)
2485 NB. This macro is set when pre-processing both Haskell source and C
2486 source, including the C source generated from a Haskell module
2487 (i.e. <Filename>.hs</Filename>, <Filename>.lhs</Filename>, <Filename>.c</Filename> and <Filename>.hc</Filename> files).
2492 <Term><Constant>__CONCURRENT_HASKELL__</Constant>:</Term>
2495 <IndexTerm><Primary>__CONCURRENT_HASKELL__ macro</Primary></IndexTerm>
2496 This symbol is defined when pre-processing Haskell (input) and
2497 pre-processing C (GHC output). Since GHC from verion 4.00 now
2498 supports concurrent haskell by default, this symbol is always defined.
2503 <Term><Constant>__PARALLEL_HASKELL__</Constant>:</Term>
2506 <IndexTerm><Primary>__PARALLEL_HASKELL__ macro</Primary></IndexTerm>
2507 Only defined when <Option>-parallel</Option> is in use! This symbol is defined when
2508 pre-processing Haskell (input) and pre-processing C (GHC output).
2516 Options other than the above can be forced through to the C
2517 pre-processor with the <Option>-opt</Option> flags (see
2518 <XRef LinkEnd="forcing-options-through">).
2522 A small word of warning: <Option>-cpp</Option> is not friendly to “string
2523 gaps”.<IndexTerm><Primary>-cpp vs string gaps</Primary></IndexTerm><IndexTerm><Primary>string gaps vs
2524 -cpp</Primary></IndexTerm>. In other words, strings such as the following:
2538 don't work with <Option>-cpp</Option>; <Filename>/usr/bin/cpp</Filename> elides the
2539 backslash-newline pairs.
2543 However, it appears that if you add a space at the end of the line,
2544 then <Command>cpp</Command> (at least GNU <Command>cpp</Command> and possibly other <Command>cpp</Command>s)
2545 leaves the backslash-space pairs alone and the string gap works as
2551 <Sect2 id="options-C-compiler">
2552 <Title>Options affecting the C compiler (if applicable)
2556 <IndexTerm><Primary>include-file options</Primary></IndexTerm>
2557 <IndexTerm><Primary>C compiler options</Primary></IndexTerm>
2558 <IndexTerm><Primary>GCC options</Primary></IndexTerm>
2562 At the moment, quite a few common C-compiler options are passed on
2563 quietly to the C compilation of Haskell-compiler-generated C files.
2564 THIS MAY CHANGE. Meanwhile, options so sent are:
2571 <ColSpec Align="Left" Colsep="0">
2572 <ColSpec Align="Left" Colsep="0">
2575 <Entry><Option>-ansi</Option> </Entry>
2576 <Entry> do ANSI C (not K&R) </Entry>
2580 <Option>-pedantic</Option> </Entry>
2581 <Entry> be so</Entry>
2585 <Option>-dgcc-lint</Option> </Entry>
2586 <Entry> (hack) short for “make GCC very paranoid”</Entry>
2594 <IndexTerm><Primary>-ansi option (for GCC)</Primary></IndexTerm>
2595 <IndexTerm><Primary>-pedantic option (for GCC)</Primary></IndexTerm>
2596 <IndexTerm><Primary>-dgcc-lint option (GCC paranoia)</Primary></IndexTerm>
2600 If you are compiling with lots of foreign calls, you may need to
2601 tell the C compiler about some <Literal>#include</Literal> files. There is no real
2602 pretty way to do this, but you can use this hack from the
2609 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
2616 <Sect2 id="options-linker">
2617 <Title>Linking and consistency-checking
2621 <IndexTerm><Primary>linker options</Primary></IndexTerm>
2622 <IndexTerm><Primary>ld options</Primary></IndexTerm>
2626 GHC has to link your code with various libraries, possibly including:
2627 user-supplied, GHC-supplied, and system-supplied (<Option>-lm</Option> math
2628 library, for example).
2635 <Term><Option>-l<FOO></Option>:</Term>
2638 <IndexTerm><Primary>-l<lib> option</Primary></IndexTerm>
2639 Link in a library named <Filename>lib<FOO>.a</Filename> which resides somewhere on the
2640 library directories path.
2644 Because of the sad state of most UNIX linkers, the order of such
2645 options does matter. Thus: <Command>ghc -lbar *.o</Command> is almost certainly
2646 wrong, because it will search <Filename>libbar.a</Filename> <Emphasis>before</Emphasis> it has
2647 collected unresolved symbols from the <Filename>*.o</Filename> files.
2648 <Command>ghc *.o -lbar</Command> is probably better.
2652 The linker will of course be informed about some GHC-supplied
2653 libraries automatically; these are:
2660 <ColSpec Align="Left" Colsep="0">
2661 <ColSpec Align="Left" Colsep="0">
2664 <Entry><Emphasis>-l equivalent</Emphasis> </Entry>
2665 <Entry> <Emphasis>description</Emphasis> </Entry>
2670 <Option>-lHSrts,-lHSclib</Option> </Entry>
2671 <Entry> basic runtime libraries </Entry>
2675 <Option>-lHS</Option> </Entry>
2676 <Entry> standard Prelude library </Entry>
2680 <Option>-lHS_cbits</Option> </Entry>
2681 <Entry> C support code for standard Prelude library </Entry>
2685 <Option>-lgmp</Option> </Entry>
2686 <Entry> GNU multi-precision library (for Integers)</Entry>
2697 <IndexTerm><Primary>-lHS library</Primary></IndexTerm>
2698 <IndexTerm><Primary>-lHS_cbits library</Primary></IndexTerm>
2699 <IndexTerm><Primary>-lHSrts library</Primary></IndexTerm>
2700 <IndexTerm><Primary>-lgmp library</Primary></IndexTerm>
2705 <Term><Option>-package <name></Option>:</Term>
2708 <IndexTerm><Primary>-package <name> option</Primary></IndexTerm>
2712 If you are using a Haskell “package” (e.g., the POSIX
2713 library), just use the <Option>-package posix</Option> option, and the
2714 correct code should be linked in. See <xref linkend="packages"> for
2720 <Term><Option>-L<dir></Option>:</Term>
2723 <IndexTerm><Primary>-L<dir> option</Primary></IndexTerm>
2724 Where to find user-supplied libraries… Prepend the directory
2725 <Filename><dir></Filename> to the library directories path.
2730 <Term><Option>-static</Option>:</Term>
2733 <IndexTerm><Primary>-static option</Primary></IndexTerm>
2734 Tell the linker to avoid shared libraries.
2739 <Term><Option>-no-link-chk</Option> and <Option>-link-chk</Option>:</Term>
2742 <IndexTerm><Primary>-no-link-chk option</Primary></IndexTerm>
2743 <IndexTerm><Primary>-link-chk option</Primary></IndexTerm>
2744 <IndexTerm><Primary>consistency checking of executables</Primary></IndexTerm>
2745 By default, immediately after linking an executable, GHC verifies that
2746 the pieces that went into it were compiled with compatible flags; a
2747 “consistency check”.
2748 (This is to avoid mysterious failures caused by non-meshing of
2749 incompatibly-compiled programs; e.g., if one <Filename>.o</Filename> file was compiled
2750 for a parallel machine and the others weren't.) You may turn off this
2751 check with <Option>-no-link-chk</Option>. You can turn it (back) on with
2752 <Option>-link-chk</Option> (the default).
2757 <Term><Option>-no-hs-main</Option>:</Term>
2760 <IndexTerm><Primary>-no-hs-main option</Primary></IndexTerm>
2761 <IndexTerm><Primary>linking Haskell libraries with foreign code</Primary></IndexTerm>
2765 In the event you want to include ghc-compiled code as part of another
2766 (non-Haskell) program, the RTS will not be supplying its definition of
2767 <Function>main()</Function> at link-time, you will have to. To signal that to the
2768 driver script when linking, use <Option>-no-hs-main</Option>.
2772 Notice that since the command-line passed to the linker is rather
2773 involved, you probably want to use the ghc driver script to do the
2774 final link of your `mixed-language' application. This is not a
2775 requirement though, just try linking once with <Option>-v</Option> on to see what
2776 options the driver passes through to the linker.
2787 <Sect1 id="sec-using-concurrent">
2788 <Title>Using Concurrent Haskell</Title>
2791 <IndexTerm><Primary>Concurrent Haskell—use</Primary></IndexTerm>
2795 GHC (as of version 4.00) supports Concurrent Haskell by default,
2796 without requiring a special option or libraries compiled in a certain
2797 way. To get access to the support libraries for Concurrent Haskell
2798 (i.e. <Literal>Concurrent</Literal> and friends), use the
2799 <Option>-package concurrent</Option> option.
2803 Three RTS options are provided for modifying the behaviour of the
2804 threaded runtime system. See the descriptions of
2805 <Option>-C[<us>]</Option>, <Option>-q</Option>, and
2806 <Option>-t<num></Option> in <XRef LinkEnd="parallel-rts-opts">.
2810 Concurrent Haskell is described in more detail in <XRef
2811 LinkEnd="sec-Concurrent">.
2816 <Sect1 id="sec-using-parallel">
2817 <Title>Using Parallel Haskell</Title>
2820 <IndexTerm><Primary>Parallel Haskell—use</Primary></IndexTerm>
2824 [You won't be able to execute parallel Haskell programs unless PVM3
2825 (Parallel Virtual Machine, version 3) is installed at your site.]
2829 To compile a Haskell program for parallel execution under PVM, use the
2830 <Option>-parallel</Option> option,<IndexTerm><Primary>-parallel
2831 option</Primary></IndexTerm> both when compiling <Emphasis>and
2832 linking</Emphasis>. You will probably want to <Literal>import
2833 Parallel</Literal> into your Haskell modules.
2837 To run your parallel program, once PVM is going, just invoke it
2838 “as normal”. The main extra RTS option is
2839 <Option>-N<n></Option>, to say how many PVM
2840 “processors” your program to run on. (For more details of
2841 all relevant RTS options, please see <XRef
2842 LinkEnd="parallel-rts-opts">.)
2846 In truth, running Parallel Haskell programs and getting information
2847 out of them (e.g., parallelism profiles) is a battle with the vagaries of
2848 PVM, detailed in the following sections.
2852 <Title>Dummy's guide to using PVM</Title>
2855 <IndexTerm><Primary>PVM, how to use</Primary></IndexTerm>
2856 <IndexTerm><Primary>Parallel Haskell—PVM use</Primary></IndexTerm>
2857 Before you can run a parallel program under PVM, you must set the
2858 required environment variables (PVM's idea, not ours); something like,
2859 probably in your <Filename>.cshrc</Filename> or equivalent:
2862 setenv PVM_ROOT /wherever/you/put/it
2863 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
2864 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
2870 Creating and/or controlling your “parallel machine” is a purely-PVM
2871 business; nothing specific to Parallel Haskell.
2875 You use the <Command>pvm</Command><IndexTerm><Primary>pvm command</Primary></IndexTerm> command to start PVM on your
2876 machine. You can then do various things to control/monitor your
2877 “parallel machine;” the most useful being:
2883 <ColSpec Align="Left">
2887 <Entry><KeyCombo><KeyCap>Control</KeyCap><KeyCap>D</KeyCap></KeyCombo></Entry>
2888 <Entry>exit <Command>pvm</Command>, leaving it running</Entry>
2892 <Entry><Command>halt</Command></Entry>
2893 <Entry>kill off this “parallel machine” & exit</Entry>
2897 <Entry><Command>add <host></Command></Entry>
2898 <Entry>add <Command><host></Command> as a processor</Entry>
2902 <Entry><Command>delete <host></Command></Entry>
2903 <Entry>delete <Command><host></Command></Entry>
2907 <Entry><Command>reset</Command></Entry>
2908 <Entry>kill what's going, but leave PVM up</Entry>
2912 <Entry><Command>conf</Command></Entry>
2913 <Entry>list the current configuration</Entry>
2917 <Entry><Command>ps</Command></Entry>
2918 <Entry>report processes' status</Entry>
2922 <Entry><Command>pstat <pid></Command></Entry>
2923 <Entry>status of a particular process</Entry>
2932 The PVM documentation can tell you much, much more about <Command>pvm</Command>!
2938 <Title>Parallelism profiles</Title>
2941 <IndexTerm><Primary>parallelism profiles</Primary></IndexTerm>
2942 <IndexTerm><Primary>profiles, parallelism</Primary></IndexTerm>
2943 <IndexTerm><Primary>visualisation tools</Primary></IndexTerm>
2947 With Parallel Haskell programs, we usually don't care about the
2948 results—only with “how parallel” it was! We want pretty pictures.
2952 Parallelism profiles (à la <Command>hbcpp</Command>) can be generated with the
2953 <Option>-q</Option><IndexTerm><Primary>-q RTS option (concurrent, parallel)</Primary></IndexTerm> RTS option. The
2954 per-processor profiling info is dumped into files named
2955 <Filename><full-path><program>.gr</Filename>. These are then munged into a PostScript picture,
2956 which you can then display. For example, to run your program
2957 <Filename>a.out</Filename> on 8 processors, then view the parallelism profile, do:
2963 % ./a.out +RTS -N8 -q
2964 % grs2gr *.???.gr > temp.gr # combine the 8 .gr files into one
2965 % gr2ps -O temp.gr # cvt to .ps; output in temp.ps
2966 % ghostview -seascape temp.ps # look at it!
2972 The scripts for processing the parallelism profiles are distributed
2973 in <Filename>ghc/utils/parallel/</Filename>.
2979 <Title>Other useful info about running parallel programs</Title>
2982 The “garbage-collection statistics” RTS options can be useful for
2983 seeing what parallel programs are doing. If you do either
2984 <Option>+RTS -Sstderr</Option><IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm> or <Option>+RTS -sstderr</Option>, then
2985 you'll get mutator, garbage-collection, etc., times on standard
2986 error. The standard error of all PE's other than the `main thread'
2987 appears in <Filename>/tmp/pvml.nnn</Filename>, courtesy of PVM.
2991 Whether doing <Option>+RTS -Sstderr</Option> or not, a handy way to watch
2992 what's happening overall is: <Command>tail -f /tmp/pvml.nnn</Command>.
2997 <Sect2 id="parallel-rts-opts">
2998 <Title>RTS options for Concurrent/Parallel Haskell
3002 <IndexTerm><Primary>RTS options, concurrent</Primary></IndexTerm>
3003 <IndexTerm><Primary>RTS options, parallel</Primary></IndexTerm>
3004 <IndexTerm><Primary>Concurrent Haskell—RTS options</Primary></IndexTerm>
3005 <IndexTerm><Primary>Parallel Haskell—RTS options</Primary></IndexTerm>
3009 Besides the usual runtime system (RTS) options
3010 (<XRef LinkEnd="runtime-control">), there are a few options particularly
3011 for concurrent/parallel execution.
3018 <Term><Option>-N<N></Option>:</Term>
3021 <IndexTerm><Primary>-N<N> RTS option (parallel)</Primary></IndexTerm>
3022 (PARALLEL ONLY) Use <Literal><N></Literal> PVM processors to run this program;
3028 <Term><Option>-C[<us>]</Option>:</Term>
3031 <IndexTerm><Primary>-C<us> RTS option</Primary></IndexTerm> Sets
3032 the context switch interval to <Literal><s></Literal> seconds.
3033 A context switch will occur at the next heap block allocation after
3034 the timer expires (a heap block allocation occurs every 4k of
3035 allocation). With <Option>-C0</Option> or <Option>-C</Option>,
3036 context switches will occur as often as possible (at every heap block
3037 allocation). By default, context switches occur every 20ms
3038 milliseconds. Note that GHC's internal timer ticks every 20ms, and
3039 the context switch timer is always a multiple of this timer, so 20ms
3040 is the maximum granularity available for timed context switches.
3045 <Term><Option>-q[v]</Option>:</Term>
3048 <IndexTerm><Primary>-q RTS option</Primary></IndexTerm>
3049 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
3050 in the file <FIlename><program>.qp</FIlename>. In the style of <Command>hbcpp</Command>, this profile
3051 records the movement of threads between the green (runnable) and red
3052 (blocked) queues. If you specify the verbose suboption (<Option>-qv</Option>), the
3053 green queue is split into green (for the currently running thread
3054 only) and amber (for other runnable threads). We do not recommend
3055 that you use the verbose suboption if you are planning to use the
3056 <Command>hbcpp</Command> profiling tools or if you are context switching at every heap
3057 check (with <Option>-C</Option>).
3062 <Term><Option>-t<num></Option>:</Term>
3065 <IndexTerm><Primary>-t<num> RTS option</Primary></IndexTerm>
3066 (PARALLEL ONLY) Limit the number of concurrent threads per processor
3067 to <Literal><num></Literal>. The default is 32. Each thread requires slightly over 1K
3068 <Emphasis>words</Emphasis> in the heap for thread state and stack objects. (For
3069 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
3075 <Term><Option>-d</Option>:</Term>
3078 <IndexTerm><Primary>-d RTS option (parallel)</Primary></IndexTerm>
3079 (PARALLEL ONLY) Turn on debugging. It pops up one xterm (or GDB, or
3080 something…) per PVM processor. We use the standard <Command>debugger</Command>
3081 script that comes with PVM3, but we sometimes meddle with the
3082 <Command>debugger2</Command> script. We include ours in the GHC distribution,
3083 in <Filename>ghc/utils/pvm/</Filename>.
3088 <Term><Option>-e<num></Option>:</Term>
3091 <IndexTerm><Primary>-e<num> RTS option (parallel)</Primary></IndexTerm>
3092 (PARALLEL ONLY) Limit the number of pending sparks per processor to
3093 <Literal><num></Literal>. The default is 100. A larger number may be appropriate if
3094 your program generates large amounts of parallelism initially.
3099 <Term><Option>-Q<num></Option>:</Term>
3102 <IndexTerm><Primary>-Q<num> RTS option (parallel)</Primary></IndexTerm>
3103 (PARALLEL ONLY) Set the size of packets transmitted between processors
3104 to <Literal><num></Literal>. The default is 1024 words. A larger number may be
3105 appropriate if your machine has a high communication cost relative to
3123 ;;; Local Variables: ***
3125 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***