Compiling Code

After you’ve written your code in Ruby, you just run it. That’s also true for Crystal. But under the hood, your code is first compiled to native code and then executed. During compilation, the Crystal compiler checks your code, and only when there are no errors, native code is generated and your code is run.

For example, the following command:

$ crystal hello_world.cr

compiles and executes the code contained in the given source file. In fact, this is a shortcut, as compilation and execution are still two separate steps. If compilation fails, nothing will be run. This way of working is nice while developing small pieces, but the compiler has to do all that work again every time, so it’s comparatively slow. To separate the compilation and execution steps, use the build option:

$ crystal build hello_world.cr

to generate a binary executable file, hello_world, which you can then execute with:

$ ./hello_world

To specify the executable filename as hello, use the –o flag:

$ crystal build –o hello hello_world.cr

For production-ready standalone executables or when performing benchmarks, you’ll need to tell the compiler to create fully optimized code, which takes somewhat longer:

$ crystal build hello_world.cr --release --no-debug

(The –no-debug flag should be temporary and no longer be necessary in Crystal v1.0.)

If the platform you’re using to do the builds doesn’t match your production environment, then you’re going to have to recompile it on the server. Another way to solve this is to deploy your app in a Docker container.

The crystal command can do a lot more. It contains a number of built-in tools, such as formatting code, generating a new project, and building its documentation. You can get an overview of what’s possible by typing crystal on the command line as shown in the figure.

images/setting_up/Fig_4.png

Explore the different compilation options with:

$ crystal build -h

Here are some things for you to try out.

Your Turn 1

a. Compile hello_world.cr to an executable and compare the different file sizes in a normal build, a release build, and a build with debug-info.

b. Compile test.cr with verbose error reporting and examine the output.

c. Greet the world in Crystal by giving the code to the compiler on the command line itself. (Hint: Use crystal eval.)

d. Find out how to view LLVM and assembler output by compiling hello_world.cr. (Don’t worry: You won’t need that for development.)

e. Try out $ crystal env to see what info this gives.

f. Experiment with icr,[128] the interactive console for Crystal. icr aims to be like irb for Ruby. It works differently under the hood because Crystal compiles instead of interpreting the code.

Clone the repository, switch to folder crystal-icr, and build it with the command: $ make.

Start it with: $ icr.

Type in a command at the icr > prompt and press ENTER to execute it. The UP ARROW gives access to previous commands, and CTRL-D exits. You’ll probably be more charmed by the Crystal Playground. See Working with Crystal Playground.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59