Windows系统如何使用Sublime Text在外部控制台运行程序?

问题描述 投票:0回答:6

我设法为 C++ 配置了 Sublime Text 2,我现在可以编译我的代码(使用 MinGW 编译器)。

不幸的是,Sublime Text 控制台不支持任何类型的 C++ 输入。

我想在外部控制台(例如 Window 的终端)中打开我编译的程序。 我尝试使用“call”或“cmd”,但无法正常工作。 (它仍然在控制台中打开文件)

这是我的构建配置:

{
"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"
}
c++ windows console mingw sublimetext
6个回答
16
投票

试试这个:

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

诀窍是使用“start”命令执行生成的文件,强制文件在新的 cmd 窗口中打开,并将所有内容放入第一个 cmd 定义中(使用 && 以执行这两个命令),因为如果你定义了多个 cmd 数组,它们就会被覆盖。


12
投票

@FacundoJ:在 Mac 上,您可以使用终端在外部运行应用程序。在 C++.sublime-build 中,更改 cmd 以便它不只是运行编译后的输出,而是在终端中启动它。

变化自:

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

致:

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

4
投票

接受的答案允许您在 Windows CMD 中执行程序并接受输入。 但是一旦程序执行结束,cmd窗口会立即关闭,你还看不清输出结果.

我认为您应该尝试以下解决方案,在执行程序后保持 CMD 窗口打开,以便您可以检查结果.

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

首先按Ctrl+B编译程序,然后按Ctrl+Shift+B并选择

run in cmd
选项,这将在系统CMD而不是Sublime Text控制台中运行程序。

以上设置在 Windows 8.1 上测试过,应该也适用于开箱即用的 Windows 7 和 Windows 10。


2
投票

在@FacundoJ 接受的答案中,每次运行时都会编译程序。这并不总是必要的(即当您只是修改一些外部资源或使用主要参数时)。在那种情况下,我推荐这个设置:

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

(假设你的路径中有 C:\MinGW)所以你可以像这样使用它

  • Ctrl+B 编译程序

  • Ctrl+Shift+B运行它(当然是在编译之后)

Note1 注意

start
命令后的空字符串:它 should be used.

Note2 你应该使用

.cpp
后缀,否则默认使用 C89 语法(至少使用 MinGW g++ 编译器)


1
投票

对于 ubuntu

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

此构建系统所做的是编译 .cpp 文件并在 xterm 中打开输出文件。

-->如果你想在 gnome-terminal 中打开输出,创建一个配置文件

在 gnome-terminal 中,转到编辑 -> 配置文件首选项 -> 单击命令选项卡。从标记为命令退出时的下拉菜单中选择“保持终端打开”。您应该为此创建一个新的配置文件

然后将“cmd”替换为

 "cmd": ["g++ ${file} && gnome-terminal --window-with-profile=PROFILENAME -e ./a.out"]

0
投票

这是工作!!

{"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}
  • 只需转到工具>构建系统>新建 系统...“C++.sublime-build”...并且必须将其保存为所有文件
  • 选择那个构建

只需输入 (ctrl+b) 即可看到提升的命令提示符打开并像其他 IDE 一样提供您的输入。

© www.soinside.com 2019 - 2024. All rights reserved.