2017-03-04 96 views
1

我正在学习使用sublime3文本编辑器编写C程序,我有两种不同的崇高版本 此版本在CMD控制台上输出它的执行;Sublime 3控制台上交互式程序的输出

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

而其它构建输出崇高控制台上其执行

> { 
"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: cannot open output file addition.exe: Permission denied collect2.exe: error: ld returned 1 exit status [Finished in 0.3s]

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

回答

0

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

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

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

为了找到问题的根源,无法直接在崇高内部运行交互式程序;或者说,你可以运行这样的程序,但是在控制台输入和你正在运行的程序之间没有连接。

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

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