gpt4 book ai didi

c - sublime 3控制台交互程序的输出

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:56 25 4
gpt4 key购买 nike

我正在学习使用 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: cannot open output file addition.exe: Permission denied collect2.exe: error: ld returned 1 exit status [Finished in 0.3s]

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

最佳答案

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

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

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

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

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

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

也可以使用 Terminus包来构建一个交互式的构建系统。自述文件包含有关如何执行此操作的文档,包括指向概述该过程的教程视频的链接。

关于c - sublime 3控制台交互程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42596417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com