2017-03-04 95 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外部運行它,以便您可以與之交互(或使用構建系統爲您執行此操作,例如您的第一個示例)。