[Compiling Bryan O'Sullivan **20080222061546] { addfile ./examples/ch06/Main.hs hunk ./en/ch06-library.xml 40 - editor, create a file named SimpleJSON.hs, and - insert the following contents. + editor, create a file named SimpleJSON.hs, + and insert the following contents. hunk ./en/ch06-library.xml 52 - SimpleJSON.hs in your editor, switch to a &ghci; - window, and load the file into &ghci;. + SimpleJSON.hs in your editor, switch to a + &ghci; window, and load the file into &ghci;. hunk ./en/ch06-library.xml 60 - SimpleJSON.hs that will extract a string from a - JSON value for us. If the JSON value actually contains a + SimpleJSON.hs that will extract a string + from a JSON value for us. If the JSON value actually contains a hunk ./en/ch06-library.xml 93 - the name of the module. By convention, the source file has the - same base name as the module, which is why our - file SimpleJSON.hs contains the module + the name of the module, which must begin with a capital letter. + By convention, a source file has the same base + name (the component before the suffix) as the + module it contains, which is why our file + SimpleJSON.hs contains the module hunk ./en/ch06-library.xml 128 + + + Compiling Haskell source + + In addition to the &ghci; interpreter; the &GHC; + distribution includes an optimising Haskell compiler, named + &ghc;. If you're already familiar with a command line compiler + such as gcc or cl (the + compiler component of Microsoft's Visual Studio), you'll + immediately be at home with &ghc;. + + To compile a source file, we first open a terminal or + command prompt window, then invoke &ghc; with the name of the + source file to compile. + + ghc -c SimpleJSON.hs + + The option tells &ghc; to only generate + object code. If we were to omit the option, + the compiler would attempt + to generate a complete executable. That would in turn fail, + because we haven't written a main function + yet. + + After &ghc; completes, if we list the contents of the + directory, it should contain two new files: + SimpleJSON.hi and + SimpleJSON.o + (SimpleJSON.obj on Windows). The former is + an interface file, in which &ghc; stores + information about the names exported from our module in + machine-readable form. The latter is an object + file, which contains the generated code. + + + + Generating a Haskell program, and importing modules + + Now that we've successfully compiled our minimal library, + we'll write a tiny program to exercise it. Create the following + file in your text editor, and save it as + Main.hs. + + &Main.hs:module; + + Notice the import directive that follows the + module declaration. This indicates that we want to take all of + the names that are exported from the SimpleJSON + module, and make them available in our module. Any + import directives must appear together at the + beginning of a module. We cannot scatter them throughout a + source file. + + Our choice of naming for the source file and function is + deliberate. In order to create an executable, &ghc; needs a + module named Main, and it must contain a function + named main. The main + function is the one that will be called when we run the program + once we've built it. + + ghc -o simple Main.hs + SimpleJSON.o + + This time around, we're omitting the + option when we invoke &ghc;, so it will attempt to generate an + executable. The process of generating an executable is called + linking. As our command line suggests, + &ghc; is perfectly able to both compile source files and link + an executable in a single invocation. + + We're passing &ghc; a new option, , + which takes one argument: this is the name of the executable + that &ghc; should create. Here, we've decided to name the + program simple. On Windows, the program + will have the suffix .exe, but on Unix + variants there will not be a suffix. + + Finally, we're passing the name of our new source file, + Main.hs, and the object file we already + compiled, SimpleJSON.o. We must explicitly + list every one of our files that contains code that should end + up in the executable. If we forget a source or object file, + &ghc; will complain about undefined symbols. + + When compiling, we can pass &ghc; any mixture of source and + object files. If &ghc; notices that it has already compiled a + source file into an object once, and we invoke it a second time, + it will only recompile the source file if we've modified + it. + + Once &ghc; has finished compiling and linking our + simple program, we can run it from the + command line. + hunk ./examples/ch06/Main.hs 1 +{-- snippet module --} +module Main () where + +import SimpleJSON + +main = print (JObject [("foo", JNumber 1), ("bar", JBool False)]) +{-- /snippet module --} }