Skip to content

Instantly share code, notes, and snippets.

@sfcgeorge
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfcgeorge/2a2e872ee7c3c092be17 to your computer and use it in GitHub Desktop.
Save sfcgeorge/2a2e872ee7c3c092be17 to your computer and use it in GitHub Desktop.
Compile Crystal code from external app
crystal run test.cr
#=> "foo"
crystal build test.cr
export CRYSTAL_PATH=/usr/local/Cellar/crystal/0.7.1/src
./test
#=>
ld: library not found for -levent
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc -o /var/folders/48/tq7k6jzd3k12q11n978s09qc0000gn/T/vector-salad-compiled.0ZfhBw .crystal/foo/main.o .crystal/foo/CFileIO.o .crystal/foo/GC.o .crystal/foo/Exception.o .crystal/foo/Pointer40UInt841.o .crystal/foo/ArgumentError.o .crystal/foo/Pointer40LibABI5858UnwindException41.o .crystal/foo/UInt64.o .crystal/foo/Array40String41.o .crystal/foo/Math.o .crystal/foo/Pointer40String41.o .crystal/foo/String.o .crystal/foo/String5858Builder.o .crystal/foo/Slice40T41.o .crystal/foo/Slice40UInt841.o .crystal/foo/IndexOutOfBounds.o .crystal/foo/DivisionByZero.o .crystal/foo/StaticArray40UInt844322241.o .crystal/foo/Array40Pointer40Void4141.o .crystal/foo/Pointer40Pointer40Void4141.o .crystal/foo/Fiber.o .crystal/foo/Pointer40Void41.o .crystal/foo/Array40Fiber41.o .crystal/foo/Pointer40Fiber41.o .crystal/foo/Event5858Base.o .crystal/foo/Scheduler.o .crystal/foo/CharReader.o .crystal/foo/InvalidByteSequenceError.o .crystal/foo/Bool.o .crystal/foo/Char.o .crystal/foo/StaticArray40UInt84432441.o .crystal/foo/Int32.o .crystal/foo/StaticArray40UInt844321241.o .crystal/foo/StringIO.o .crystal/foo/LEBReader.o .crystal/foo/Exception43.o .crystal/foo/Errno.o .crystal/foo/GlobError.o .crystal/foo/IO5858EOFError.o .crystal/foo/IO5858Error.o .crystal/foo/TimeFormat5858Error.o .crystal/foo/MissingKey.o .crystal/foo/DomainError.o .crystal/foo/EmptyEnumerable.o .crystal/foo/AtExitHandlers.o -levent -lpcl -lpcre -lgc -lpthread`
require "compiler/crystal/**"
def tempfile(basename)
tempfile = Tempfile.new "vector-salad-#{basename}"
output_filename = tempfile.path
tempfile.close
output_filename
end
def execute(output_filename)
exit_status = LibC.system("#{output_filename}")
if exit_status != 0
puts "Program terminated abnormally with error code: #{exit_status}"
exit 1
end
File.delete output_filename
end
sources = Crystal::Compiler::Source.new("foo", %(p "foo"))
output_filename = tempfile "compiled"
compiler = Crystal::Compiler.new
compiler.compile sources, output_filename
execute output_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment