2

I wrote a program to print the sum of two numbers in Elixir. I saved the file as solution.ex and when I compile it using elixirc as elixirc solution.ex it both compiles and executes the code. I thought that elixirc will only compile the code and generate a binary and running the binary using elixir will execute it. Any help will be appreciated.

defmodule Solution do
  defp sum(a, b), do: a + b

  def main() do
    a = IO.gets("") |> String.strip |> String.to_integer
    b = IO.gets("") |> String.strip |> String.to_integer
    sum(a, b) |> IO.puts
  end
end

Solution.main()
2
  • If either of our answers have answered your question @SaiKiran would you please accept one of them? If not maybe you can elaborate on what either of us have missed? Commented Mar 4, 2016 at 19:01
  • @OnorioCatenacci sorry i was caught up with work. i haven't reviewed it but once i do, i'll surely do accept one of them. Just need some time to look into what you have told. Commented Mar 4, 2016 at 19:48

2 Answers 2

2

Elixir has to run the code to compile it; that's just how it works. Compiling that file will correctly generate Solution.beam (bytecode for the Solution module) because it executes the defmodule macro. After that, it will blindly run Solution.main(). (just for clairty, the code inside defs is not run at compile-time)

If you want to build an executable out of this that you can call like $ elixir-sum, then you may want to look at escripts, which are executable Erlang (and therefore Elixir) scripts (that still need the Erlang VM installed in order to run). You can read more information about them in the documentation for the mix escript.build task or in a bunch of blog posts such as this one.

Roughly, you'd need to add this to the configuration of your project (returned by project/0 in your mix.exs file):

def project() do
  [...,
   main_module: Solution]
end

This way, the generated script will call the main/1 function of the Solution module passing the command-line arguments to it. Note that you don't have such a function, you only have main/0 (which takes no arguments), so you'd need to define main/1 (where you can just ignore the arguments).

Sign up to request clarification or add additional context in comments.

Comments

1

Just in case your question is how to keep elixirc from running the code, this is the answer:

defmodule Solution do
  defp sum(a, b), do: a + b

  def main() do
    a = IO.gets("") |> String.strip |> String.to_integer
    b = IO.gets("") |> String.strip |> String.to_integer
    sum(a, b) |> IO.puts
  end
end

#Solution.main()

As @whatyouhide said the code will get run regardless. But without the Solution.main() at the end of your code, it will simply compile the code and have nothing to execute so it will exit. So either comment out Solution.main() or remove it.


EDIT: Further comments by the original poster make me think that what he or she is looking for is the ability to take the compiled binary to a different machine and run it. If that's the case then what you probably want is exrm.

Elixir (and indeed Erlang) don't simply build a single binary image. Both of them depend on runtime binaries to execute as well. Exrm will figure out all the runtime files you need and bundle them into a single compressed file that you can move to the machine where you want to execute.

But there's not a single binary file that you can simply deploy on a different machine. At the least you'll also need the Erlang and Elixir runtimes.

3 Comments

what i want to do is automate the whole thing. Compile the elixir code, get the binary and execute it where the input will be given from files. to be put plain and simple i want a command which compiles and gives the binary and then command which will execute it and print the output.
Then instead of elixirc use elixir.
but elixir compiles and runs it. i need to get a executable file after compiling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.