miiastack
miiastack

Reputation: 51

How to compile single file in elixir?

How to compile to executable in terminal file an elixir file?

I read that to compile elixir I need to create new project. But file, that I want to compile is IO.puts "hello world".

Upvotes: 5

Views: 17443

Answers (3)

Marco Prins
Marco Prins

Reputation: 7419

You can compile an elixir file using the elixirc command.

So if you have a file named file.ex, you can run:

elixirc file.ex

This will create a .beam file in the current directory for each module defined in your file. If you start iex in this directory your modules from the file will be available.

Here you can read more about this command in the official guides.

Upvotes: 3

user6029982
user6029982

Reputation:

You can also execute from command line. Save the file with a .exs extension (file.exs).

For example file.exs contains this line of code:

IO.puts "Hello World"

Then execute it with elixir from command line like this:

$ elixir file.exs
Hello World

Elixir website has helpful instructions

Hope this helped!

Upvotes: 1

denis.peplin
denis.peplin

Reputation: 9851

To compile from shell, first create a file:

# module_name.ex
defmodule ModuleName do
  def hello do
    IO.puts "Hello World"
  end
end

Then run shell and compile:

Interactive Elixir
iex> c("module_name.ex")
[ModuleName]
iex> ModuleName.hello
Hello world!
:ok

Code is copied from A Crash Course - Elixir

If you want to use elixirc, read this answer: https://stackoverflow.com/a/31485826/1173020

Upvotes: 3

Related Questions