【问题标题】:Output of interactive program on sublime 3 console在 sublime 3 控制台上输出交互式程序
【发布时间】:2017-03-04 13:11:53
【问题描述】:

我正在学习使用 sublime3 文本编辑器编写 C 程序,我有两个不同的 sublime 版本 此构建在 CMD 控制台上输出其执行;

> {
"cmd": ["gcc", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k",  "$file_base_name"],
"selector": "source.c",
"working_dir": "${file_path}",
"shell": true
}

而另一个构建在 sublime 控制台上输出其执行

> {
"cmd": ["gcc", "$file_name", "-o", "$file_base_name"],
"selector": "source.c",
"working_dir": "${file_path}",


"variants":
    [
        {
            "name": "Run",
            "cmd": ["gcc","${file}", "-o", "$file_base_name", "&&", "$file_path/$file_base_name"],
            "shell":true
        }
    ]
}

我更喜欢第二个构建,但它仅在您使用它来运行非交互式程序时才有效,例如 "Hello world" 程序,当我使用它来运行像这样的简单交互式程序时它会给我错误消息:

#include <stdio.h>
#include <float.h>


// variable declaration;

int main()
{   
    float a,b,sum;

    printf("Enter value of a\n");
    // for float u use "f" instead of d
    scanf("%f", &a);
    printf("Enter value of b\n");   
    scanf("%f",&b);
    sum=a+b;
    printf("The sum of %f and %f = %f\n",a,b,sum);

    return 0;
}

错误信息:

c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: 无法打开输出文件addition.exe:权限被拒绝collect2.exe: 错误:ld 返回 1 个退出状态 [0.3s 完成]

我想知道我是否可以在 sublime 控制台上执行交互式程序,或者我需要对我的 sublime 构建做一些事情。

【问题讨论】:

    标签: c mingw sublimetext3


    【解决方案1】:

    短版:不,您不能在 Sublime 中运行交互式程序(并与之交互)。

    来自链接器的错误消息告诉您它想要创建/修改文件addition.exe 但不允许这样做,因为操作系统(在本例中为 windows)告诉它它不能。

    其根本原因是 Windows 不允许您删除/修改当前正在使用的文件,这在这种情况下意味着它正在运行。

    要解决问题的关键,直接在 sublime 中运行交互式程序是不可能的;或者更确切地说,您可以运行这样的程序,但控制台中的输入与您正在运行的程序之间没有任何联系。

    这意味着当您运行该程序时,您无法与它进行交互,但它仍然坐在那里等待您输入某些内容。然后,当您修改代码并尝试再次构建和运行它时,您会收到您在此处看到的错误,因为可执行文件仍在运行且无法修改。在这种情况下,您可以取消构建以停止正在运行的程序。

    为了运行这样的程序,您需要在 Sublime 之外运行它,以便您可以与之交互(或使用为您执行此操作的构建系统,例如您的第一个示例)。

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 2014-02-28
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      相关资源
      最近更新 更多