How to compile and run a code with Sublime Text

How to compile and run a code with Sublime Text

You can use the inbuilt Build option of Sublime Text to ease your productivity. Many of the new developers find it troublesome to compile and run a program, even when they have to work with a single file. Sublime Text Build option is a lifesaver for them.

How to Create a Build System in Sublime Text

On the standard menu, goto Tools > Build Systems > New Build System...

How to Create a New Build System with Sublime Text

It opens up a new file with a .sublime-build extension, having some basic definitions. Replace that with the following build rules of your choice and save it with a preferred name. (After saving the file all you have to do is to press Ctrl + B, to build and run)

How to Create the New Build System with Sublime Text

Save the following configurations with a preferred file name in the same destination to use it.

Save the following configurations with a preferred file name in the same destination to use it.

*Keep it noted that the path variables has to be pre-set in order to run the build systems properly.

Java: Build System to compile and run a .java file

{
    "cmd": ["javac", "$file_name","&&","java", "$file_base_name"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java",
    "shell": true
}

C: Build System to compile and run a .c file

{
    "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
    "selector" : "source.c",
    "shell" : true,
    "working_dir" : "$file_path"
}

C++: Build System to compile and run a .cpp file

{
    "cmd" : ["g++", "$file_name", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
    "selector" : "source.c",
    "shell":true,
    "working_dir" : "$file_path"
}

CUDA: Build System to compile and run a .cu file

{
    "cmd" : ["nvcc", "$file_name", "-o", "${file_base_name}.exe","-m32" ,"&&", "${file_base_name}.exe"],
    "selector" : "source.cu",
    "shell":true,
    "working_dir" : "$file_path"
}

*note the '-m32' argument. It is used to compile 64-bit cuda in 32-bit cl.exe (of Visual Studio). If any problems come out. Ask them in your comments.

Verilog: Build System to compile and run a .v file

{
    "cmd": ["iverilog", "$file_name", "-o", "${file_base_name}.vvp", "&&","vvp", "${file_base_name}.vvp"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.v",
    "shell": true,
    "working_dir" : "$file_path",
    "variants": [
    {
        "cmd": ["iverilog", "$file_name", "-o", "${file_base_name}.vvp"],
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.v",
        "name": "Verilog Compile"
    },
    {
        "cmd": ["vvp", "${file_base_name}.vvp"],
        "name": "Run Verilog"
    }
    ]
}

How to Run a Build System in Sublime Text

Run the build system using the shortcut keys Ctrl + B. If several build systems are there for a single file extension, first you'll have to select which build system to use before continuing.

Happy Coding!

Ishan

Ishan Madhusanka is a dynamic developer, with a great enthusiasm in Web and Mobile Development. Eventhough he is a developer, he has this awesome creativity at its apex which helps him to polish up the UI/UX aspect of his products

1 comment: