【问题标题】:Sublime text 3 - compile program and run in terminalSublime text 3 - 编译程序并在终端中运行
【发布时间】:2014-02-07 09:13:21
【问题描述】:

我使用的是 Ubuntu 12.04,我想知道是否可以从终端自动运行 c++ 程序?当您必须在控制台中使用构建时,它真的很糟糕,因为有时我会意外地进行无限循环并且必须重新启动 sublime text 才能再次工作。 我正在使用 Sublime text 3。

【问题讨论】:

  • 终端是什么意思?我(强烈)怀疑答案是肯定的,但我不确定你的意思。
  • 终端,我的意思是命令行界面终端,不知道怎么解释更清楚。这是图片:i.stack.imgur.com/8GaCZ.png
  • 是的。您可以使用 Build automation 执行构建,或者只运行 make 或 cmake(取决于您的构建方式)。
  • 如果您的程序处于无限循环中,您不必关闭 SublimeText。你不能在终端窗口中直接killall <program_name> 吗?

标签: c++ ubuntu compiler-construction sublimetext3


【解决方案1】:

Sublime Text 3 包含两个您可能感兴趣的构建系统:C++ 和 Make。 C++.sublime-build文件如下:

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

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}

要使用它,请转到Tools -> Build System 并选择C++。您现在可以使用 CtrlB 来运行构建(顶部命令),或 CtrlShiftB 运行Run 变体。

【讨论】:

  • 在 windows 上哪里可以找到 C++.sublime-build 文件?
  • @CalleBergström 你需要使用PackageResourceViewer 插件。有关使用说明,请参阅我的回答 here。但是,不要选择Extract Package,而是选择Open Resource,然后选择C++,然后选择C++.sublime-build(也可以命名为C++ Single File.sublime-build,具体取决于您拥有的ST3 版本)。
  • 这不会在终端中运行程序。 OP 询问如何在终端中运行程序。
【解决方案2】:
{
  "cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
  "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c, source.c++, source.cxx, source.cpp",
  "variants":
  [
      {
          "name": "Run",
          "shell": true,
          "cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\"'"]
      }
  ]    
}

它可以在终端中运行并从键盘输入数据

【讨论】:

  • @jdhao 我没有在 CentOS 中测试过,它在 Ubuntu 17.04 中运行良好
【解决方案3】:

我认为接受的答案没有达到 OP 想要达到的目标。 OP 想知道如何在终端中执行当前文件

@Flycode 的设置对我不起作用。我使用的是 CentOS 7 和 Sublime Text 3。由于人们可能使用不同的终端模拟器,所以我列出了不同终端的不同设置。

注意

以下设置在上述环境下测试,效果良好。我不能保证他们会在其他环境中工作。如果它不适合你,请告诉我。

选项 1:GNOME 终端

您可以使用以下设置,

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

    "variants":
    [
        {
            "name": "Run",
          "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'",
        }
    ]
}

gnome-terminal会自动关闭执行窗口,上面的命令

   "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'" 

以这种方式使用以确保我们可以看到执行结果。有关如何防止 gnome-terminal 自动关闭的详细讨论,请参阅 this SO post

选项 2:XTerm

您可以使用以下设置(为简洁起见,我省略了一些设置)

{    // same stuff as option 1
    "variants":
    [
        {
           "name": "Run",
            //use this if you want to input other command after programm execution
           "shell_cmd": "xterm -e '${file_path}/${file_base_name}; bash'",
           //or you can use the below setting if you just want to execute this program
           // "shell_cmd": "xterm -hold -e ${file_path}/${file_base_name}",

        }
    ]
}

请参阅this SO post,了解如何防止 xterm 窗口自动关闭。

选项 3:控制台

您可以使用以下设置,

{    // same stuff as option 1
        "variants":
        [
            {
                "name": "Run",
                "shell_cmd": "konsole --hold -e ${file_path}/./${file_base_name}",        
            }
        ]
}

请参阅此处和here 讨论在执行程序后保持 konsole 窗口。

【讨论】:

    【解决方案4】:

    Tools >> Build System >> New Build System 然后粘贴这个并按 Ctrl+S 保存文件。 现在转到工具 >> 构建系统 >> 选择您的文件 >> 现在编写您的代码 >> 按 Ctrl+B >> 选择在终端中运行以进行构建并运行您的代码

    {
    "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "shell": true,
    "working_dir": "${file_path}",
    "selector": "source.c++, source.cpp, source.cc, source.cxx",
    "variants":
    [
        {
            "name": "Run in Terminal",
            "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal    
        }
    ]
    

    }

    【讨论】:

      【解决方案5】:

      通过此构建,您可以通过按 ctrl+shift+B 直接从 subime 运行 C/C++ 程序。

      它允许用户在运行时输入。

      当您直接通过它运行时,它还通过在终端窗口上显示错误来帮助调试。

      { 
         "cmd": "g++ \"${file}\" -o \"${file_path}\\\\${file_base_name}\"",
         "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
         "working_dir": "${file_path}", 
         "selector": "source.c,source.c++,source.cpp",
         "shell":true,
         "variants": [
         { 
             "name": "Run",
              "cmd" : ["gnome-terminal -- bash -c \"g++ $file_name ;echo -------------output--------------; ./a.out;echo;echo;  echo Press ENTER to continue; read line;exit; exec bash\""
           ],
         }
       ]
      }
      

      【讨论】:

        【解决方案6】:

        在 Mac 上,我使用 fswatch(我确信在 Linux 上有类似的东西)在保存时自动构建和运行测试用例。

        【讨论】:

          【解决方案7】:

          这是我编译和运行 C++ 程序的配置。程序从文件“input.txt”获取输入并将输出打印到“output.txt”。当前工作目录中的两个文件。
          操作系统:ubuntu 16
          崇高 3
          -> “工具 > 构建系统 > 新构建系统”并复制以下设置

          {
          "shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" ",
          "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
          "shell": true,
          "working_dir": "${file_path}",
          "selector": "source.c++, source.cxx, source.cpp, source.cc",
          
          "variants":
          [
              {
                  "name": "Run",
                "shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name} < input.txt > output.txt \"'",
              }
          ] }
          

          【讨论】:

            【解决方案8】:

            在目录工具>>构建系统>>新构建系统中。 创建新文件。现在也可以输入了。

            {

            "cmd": ["g++", "-Wall", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
            "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
            "working_dir": "${file_path}",
            "selector": "source.c, source.c++",
            
            "variants":
            [
                {
                    "name": "Run",
                    "cmd": ["x-terminal-emulator", "-e", "bash", "-c", "g++ -Wall -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'; read -p 'Press any key to continue...'"]
                }
            ]
            

            }

            【讨论】:

              猜你喜欢
              • 2017-10-18
              • 1970-01-01
              • 1970-01-01
              • 2014-08-05
              • 2015-12-01
              • 2013-01-15
              • 1970-01-01
              • 2014-02-05
              • 1970-01-01
              相关资源
              最近更新 更多