Editors Choice

3/recent/post-list

Sublime Text Build Configuration for C++ with External Console

Sublime Text Build Configuration for C++ with External Console (Windows & Linux)

Are you tired of running C++ programs in Sublime Text's built-in console? Do you want an external terminal for better debugging and input handling? In this guide, I'll show you how to set up a Sublime Text Build System for C++ that compiles and runs your code in an external console—perfect for Windows and Linux users!

Why Use an External Console?

  • Better input handling (no more issues with cin or scanf)
  • Clearer error messages (no disappearing console)
  • Easier debugging (keeps the terminal open after execution)

Step 1: Create a Custom Build System in Sublime Text

  1. Open Sublime Text
  2. Go to:
    • Tools → Build System → New Build System...
  3. Paste the following JSON configuration (for Windows):
{
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "shell_cmd": "start cmd /c \"\"${file_path}/${file_base_name}.exe\" & pause\""
        },
        {
            "name": "Build & Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}.exe\" && start cmd /c \"\"${file_path}/${file_base_name}.exe\" & pause\""
        }
    ]
}

For Linux Users:

Replace the "Run" and "Build & Run" variants with:

{
    "name": "Run",
    "shell_cmd": "gnome-terminal -- bash -c '\"${file_path}/${file_base_name}\"; read -p \"Press Enter to exit\"'"
},
{
    "name": "Build & Run",
    "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -- bash -c '\"${file_path}/${file_base_name}\"; read -p \"Press Enter to exit\"'"
}
  1. Save the file as C++_External_Console.sublime-build

Step 2: Where to Save the Build File?

On Windows:

Save in:

C:\Users\<YourUsername>\AppData\Roaming\Sublime Text\Packages\User\

On Linux:

Save in:

~/.config/sublime-text/Packages/User/

Step 3: Using the Build System

  1. Open a C++ file in Sublime Text.
  2. Select the build system:
    • Tools → Build System → C++_External_Console
  3. Run your code:
    • Ctrl + B → Just Build
    • Ctrl + Shift + B → Choose "Build & Run" or "Run"

Bonus: Troubleshooting

"g++ not found"?

  • Install MinGW (Windows) or G++ (Linux)
  • Windows: Download MinGW
  • Linux: Run sudo apt install g++

External terminal not opening?

  • On Linux, ensure gnome-terminal is installed (sudo apt install gnome-terminal)
  • On Windows, check if cmd is accessible

Conclusion

Now you can compile and run C++ programs in Sublime Text with an external console—no more disappearing terminals! 🎉

🔹 Pro Tip: Bookmark this guide for future reference!

Did this work for you? Let me know in the comments! 🚀

Post a Comment

0 Comments