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

泪湿孤枕 提交于 2019-12-17 15:53:09

问题


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"
}

回答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.




回答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}'"]



回答3:


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.




回答4:


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)




回答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"]



回答6:


This is WORKING !!

{"cmd": ["g++", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o", 
"${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"shell": true}
  • Just goto Tools>Build Sytem>New build System..."C++.sublime-build"...and Must SAVE it as ALL FILES
  • Select that build

Just enter (ctrl+b) to see an elevated command prompt open and give your inputs like other IDE's.



来源:https://stackoverflow.com/questions/11601423/how-to-run-programs-in-an-external-console-with-sublime-text-in-windows-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!