module Crystal
Defined in:
crystal/datum.crcrystal/dwarf.cr
crystal/dwarf/abbrev.cr
crystal/dwarf/info.cr
crystal/dwarf/line_numbers.cr
crystal/dwarf/strings.cr
crystal/elf.cr
crystal/main.cr
Constant Summary
- BUILD_COMMIT =
"4e6c0f26e"
- BUILD_DATE =
"2021-10-21"
- CACHE_DIR =
"/root/.cache/crystal"
- DEFAULT_PATH =
"$ORIGIN/../share/crystal/src"
- DESCRIPTION =
"Crystal 1.2.1 [4e6c0f26e] (2021-10-21)\n\nLLVM: 10.0.0\nDefault target: x86_64-unknown-linux-gnu"
- LIBRARY_PATH =
"/usr/bin/../lib/crystal"
- LLVM_VERSION =
"10.0.0"
- PATH =
"lib:/crystal/src"
- VERSION =
"1.2.1"
Class Method Summary
- .main(&)
Defines the main routine run by normal Crystal programs:
- .main(argc : Int32, argv : Pointer(Pointer(UInt8)))
Main method run by all Crystal programs at startup.
- .main_user_code(argc : Int32, argv : Pointer(Pointer(UInt8)))
Executes the main user code.
Class Method Detail
def self.main(&)Source
Defines the main routine run by normal Crystal programs:
- Initializes the GC
- Invokes the given block
- Handles unhandled exceptions
- Invokes
at_exit
handlers - Flushes
STDOUT
andSTDERR
This method can be invoked if you need to define a custom main (as in C main) function, doing all the above steps.
For example:
fun main(argc : Int32, argv : UInt8**) : Int32 Crystal.main do elapsed = Time.measure do Crystal.main_user_code(argc, argv) end puts "Time to execute program: #{elapsed}" end end
Note that the above is really just an example, almost the same can be accomplished with at_exit
. But in some cases redefinition of C's main is needed.
def self.main(argc : Int32, argv : Pointer(Pointer(UInt8)))Source
Main method run by all Crystal programs at startup.
This setups up the GC, invokes your program, rescuing any handled exception, and then runs at_exit
handlers.
This method is automatically invoked for you, so you don't need to invoke it.
However, if you need to define a special main C function, you can redefine main and invoke Crystal.main
from it:
fun main(argc : Int32, argv : UInt8**) : Int32 # some setup before Crystal main Crystal.main(argc, argv) # some cleanup logic after Crystal main end
The Crystal.main
can also be passed as a callback:
fun main(argc : Int32, argv : UInt8**) : Int32 LibFoo.init_foo_and_invoke_main(argc, argv, ->Crystal.main(Int32, UInt8**)) end
Note that before Crystal.main
is invoked the GC is not setup yet, so nothing that allocates memory in Crystal (like new
for classes) can be used.
def self.main_user_code(argc : Int32, argv : Pointer(Pointer(UInt8)))Source
Executes the main user code. This normally is executed after initializing the GC and before executing at_exit
handlers.
You should never invoke this method unless you need to redefine C's main function. See Crystal.main
for more details.
© 2012–2021 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.2.1/Crystal.html