Open In App

Compiling Dart Program using multiple options

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Dart programming language is owned by Google and it is widely used for web development, desktop apps, and Android apps in the form of Flutter tool which is also owned by Google. Flutter is the cross-platform framework of Dart, which means Flutter can run its code on multiple platforms so that’s why it need different code for different platform. To achieve this requirement Google had a language named Dart which runs on multiple platforms but to do it we need a different compilation of the Dart program. So in this article, we are doing it for multiple platforms using the dart compile command for different platforms.

Steps to Create Dart Program

Follow the below-specified steps to create the Dart Program:

Step 1: Write the following code into the file name as geeks.dart:

Dart




void main() {
  print('Hello World!');
}


Step 2: Open the terminal and run the following command you will get the multiple available options to run the dart program which is specially designed for various platforms.

dart compile

Hit enter and you will get multiple options in the following format:

Dart Compile

So let’s discuss all the subcommands to compile the Dart Program.

Different Options to Compile Dart Program

1. aot-snapshot

AOT stands for Ahead of time. This AOT module is used when we are deploying the command-line application, while deploying the application, the size should be minimal as compared to the local machine size. So to reduce the disk space AOT module is used. The art-snapshot subcommand is used to produce an output file specific to the system where we compile our application file. It means that the output file depends on the architecture of the system. If you are using Linux to compile file the .aot file is strictly specific to Linux only and for other Operating System also.

Command:

dart compile aot-snapshot geeks.dart

2. exe

The exe sub command generates the .exe file which is a standalone executable for Windows, macOS, and Linux distros. That generative .exe contains native machine code which is only understandable to the specific machine where it is compiled. If we compiled a dart file using dart compile exe geeks.dart on Linux then it only works on Linux and not on Windows and macOS. The output file generated by it (geeks.exe) has a small Dart runtime which is responsible for handling type checking and garbage collection.

Command:

dart compile exe geeks.dart

Using exe for compilation

3. jit-snapshot

The jit-snapshot option for running the dart program is the default option, whenever we do not give any option it will run on this option only. The file extension is .jit of the generated file. The performance of JIT is faster than AOT (ahead of time) module.

Command:

dart compile jit-snapshot geeks.dart

Using jit-snapshot for compilation

4. js

To support the web, dart converts its program into javascript, and for that if we want explicitly JavaScript code then better to go for this option. The following three files are generated out.js, out.js.deps, and out.js.map which are the dependent files for out.js. These files are responsible for supporting the dart to the web environment.

Command:

dart compile js geeks.dart

Using js for compilation

5. kernel

kernel option generates a .dill file which is a binary file. This module will run on any platform that has Dart runtime installed. This option is helpful for the deployment of the application because its size is smaller than the actual size of the project and it is faster to execute. The generated file extension is *.dill and here * will be replaced by our source file name.

Command:

dart compile kernel geeks.dart

Using kernel for compilation

Conclusion

Dart programs to target different platforms. By using the ‘dart compile‘ command with different subcommands, such as ‘aot-snapshot,’ ‘exe,’ ‘jit-snapshot,’ ‘js,’ and ‘kernel,’ developers can generate output files tailored for specific use cases and platforms. Whether you need ahead-of-time compilation for minimal disk space, standalone executables, web support through JavaScript, or efficient deployment with smaller binary files, Dart provides a range of compilation options to meet your project’s requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads