Convert your Julia Project into an Executable

Convert your Julia Project into an Executable

·

3 min read

If you want your Julia project to work as an executable, it is now possible with the exe.jl package. Using this package, you can easily convert any Julia project into an executable file.

NOTE: You will need to organize your Julia code according to some conventions and this package is tested only on UNIX-like operating systems.

Features

You can easily use command line argument in your Julia code with the native Vector{String} type.

Prerequisites

To make the executable of your project you will need some tools which are as follows:

  • GCC or Clang Compiler

  • Julia [Installed]

  • A Specific Convention:

    To get things done, you will need to organize your project code around a main function in the base project file (in my case it is main.jl). My demo project myproject looks something like this:

    myproject

    └── main.jl

    0 directories, 1 file

    and the contents of main.jl file is-

      function main(argc::Int32, argv::Vector{String})
          println("argc: $argc")
          println("argv: $argv")
      end
    

    Here, argc is the number of command line arguments passed and the argv is an array of all those arguments.

Usage

Here are some steps to make the executable of your project:

  1. Move the setup.jl file from the exe.jl repository to your project directory.

    e.g. Suppose I have a project folder 'myproject' and the 'exe.jl' repository in the home directory of my system.

     $ mv ~/exe.jl/setup.jl ~/myproject/
    
  2. Run the setup.jl file using the installed Julia binary.

     $ julia setup.jl
    
  3. When you will run the latter command, you will be asked for the Project's base file which in this case is main.jl.

     $ julia setup.jl
     Project File: main.jl
    

    after you have entered the name of the base file: 'main.jl', you will see Search main.jl: OK status if the file exists else Search main.jl: ERROR

    After this, you will be asked for Julia Path

      $ julia setup.jl
     Project File: main.jl
     julia Path: /opt/julia-1.6.7/ #in my case
    

    Here, you have to type the path of your Julia folder. You can find the path using the which command:

     $ which julia
     /opt/julia-1.6.7/bin/julia #in my case
    

    From this path, you have to take only the main path, which is /opt/julia-1.6.7/.

    NOTE: Version number may differ.

  4. If the above commands run without showing any error or warning messages. You can now see an executable file in the project directory with the name app.

     $ ls
     app main.jl setup.jl
    
  5. Now, You can execute this file.

     $ ./app hello world
     argc: 3
     argv: ["./app", "hello", "world"]
    

Conclusion

Making an executable of Julia code seems to be complex, but using the exe.jl you can easily make it by just running a setup file and passing some information.