How to run programs in an external console with Sublime Text in Windows system?

22,621

Solution 1

Try with this:

{
    "cmd": ["C:\\Dev-Cpp\\bin\\mingw32-g++.exe", "-static", "-Wall", "-time", "$file", "-o", "$file_base_name.exe", "&&", "start", "$file_base_name"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "working_dir": "${project_path:${folder}}",
    "selector": "source.c",
    "shell": true,
    "encoding": "latin1"
}

The trick is to execute the generated file using the "start" command, which forces the file to open in a new cmd window, and to put everything inside the first cmd definition (using the && in order to execute the two commands), because if you define more than one cmd array, they overwrite.

Solution 2

@FacundoJ: On Mac, you can use Terminal to run the app externally. In C++.sublime-build, change the cmd so that instead of just running the compiled output, it launches it in Terminal.

Change from:

"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]

To:

"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]

Solution 3

In the accepted answer from @FacundoJ you compile the program everytime you run it. This is not always necessary (i.e. when you just modify some external resources or play with main args). In that case I recommend this settings:

{
  "cmd": ["g++", "$file", "-o", "$file_base_name.exe"],
  "working_dir": "${project_path:$folder}",
  "selector": "source.cpp",
  "shell": true,
  "variants": [{
    "name": "Run",
    "cmd": ["start", "", "$file_base_name"]
  }]
}

(Assuming you have C:\MinGW\bin in your PATH) so you can use it like

  • Ctrl+B Compile the program

  • Ctrl+Shift+B Run it (after compiled, of course)

Note1 Note the empty string after start command: it should be used.

Note2 you should use .cpp suffix, or C89 syntax is used by default (at least with MinGW g++ compiler)

Solution 4

The accepted answer allows you to execute the program in Windows CMD and accept input. But once the program execution is over, the cmd window will be closed immediately before you can see clearly the output.

I think you should try the following solution, which keeps the CMD window open after executing the program, so that you can examine your result.

{
    "shell_cmd": "g++ -Wall -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",

    "variants":
    [   // run in the sublime text console
        {
            "name": "Run",
            "shell_cmd":"\"${file_path}/${file_base_name}\""
        },
        // run in the Windows cmd
        {
            "name": "Run in cmd",
             // the following two settings are all valid, choose one you prefer
            "shell_cmd":   "start cmd /k  $file_base_name "
            // "shell_cmd":   "start \"$file_base_name\" call $file_base_name"
        }
    ]
}

First press Ctrl+B to compile the program, then press Ctrl+Shift+B and choose run in cmd option, which will run the program in system CMD instead of the Sublime Text console.

The above settings are tested on Windows 8.1, and should also work on Windows 7 and Windows 10 out of the box.

Solution 5

For ubuntu

{
"cmd": ["g++ ${file} && xterm -hold -e ./a.out"], 
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"shell": true,
}

what this build-system does is compiles the .cpp file and open the output file in xterm.

-->if you want the output to open in gnome-terminal,create a profile

In gnome-terminal, go to Edit -> Profile Preferences->Click the Command tab. Select 'Hold the terminal open' from the drop-down menu labelled When command exits.You should create a new profile for that

and then replace "cmd" with

 "cmd": ["g++ ${file} && gnome-terminal --window-with-profile=PROFILENAME -e ./a.out"]
Share:
22,621
halflings
Author by

halflings

I'm a Python developer (and Machine Learning student) who likes data, challenges and silly tweets.

Updated on July 05, 2022

Comments

  • halflings
    halflings almost 2 years

    I managed to configure Sublime Text 2 for C++ and I can now compile my code (using the MinGW compiler).

    Unfortunately, the Sublime Text console can't support any kind of input for C++.

    I want to open my compiled program in an external console (Window's terminal for instance). I tried to use "call" or "cmd" but couldn't make it work. (it still opens the file in the console)

    Here's my build configuration:

    {
    "cmd": ["C:\\MinGW\\bin\\mingw32-g++.exe", "-Wall", "-time", "$file", "-o", "$file_base_name"],
    "cmd": ["call", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "working_dir": "${project_path:${folder}}",
    "selector": "source.c",
    "shell": true,
    "encoding": "latin1"
    }