gpt4 book ai didi

无法使用 perl 中的系统命令获取已编译 C 程序执行的标准错误

转载 作者:行者123 更新时间:2023-12-04 02:28:04 25 4
gpt4 key购买 nike

我写了一个不错的小 perl 脚本,对我来说非常有用。它允许我编译和执行 C 指令,就好像它是解释语言的指令一样。它是我用来学习 C 语言的 C 编程 IDE。

下面是我的使用方法:

crepl -Istdio 'char * str="text"; printf("%s\n", str);'
OUTPUT
text

crepl -Istdio,string 'char * str="text"; int i; for(i=0; i < strlen(str);i++){printf("%c", str[i]);} printf("\n");'
OUTPUT
text

crepl -Istdio 'char * str="text"; int i=0; while(str[i] != '\''\0'\''){printf("%c", str[i]); i++;} printf("\n");'
OUTPUT
text

这是脚本:

#!/usr/bin/perl

# this script requires this line in /etc/fstab :
#tmpfs /tmp/ram/ tmpfs defaults,noatime,nosuid,nodev,mode=1777,size=32M 0 0

use strict;
use warnings;
use autodie;

my @include;
$,="\n";
$\="\n";

if (not @ARGV) { exit }
elsif ($ARGV[0] =~ m/^-I/) {
$ARGV[0] =~ s/^-I//;
@include = split /,/, $ARGV[0];
shift;
}

#my $source_code = "/tmp/ram/c_program.c"; # requires adding a line in /etc/fstab
#my $compiled_code = "/tmp/ram/c_program.o"; # requires adding a line in /etc/fstab
my $source_code = "./.c_program.c";
my $compiled_code = "./.c_program.o";

open my $FH, ">", $source_code;
foreach (@include){
print $FH "#include <$_.h>";
}
print $FH "int main(int argc, char *argv[]) {";
print $FH $ARGV[0];
print $FH "return 0;";
print $FH "}";
close $FH;

system "gcc", $source_code, "-o", $compiled_code;
system $compiled_code;

我遇到的问题是我没有收到运行时发生的错误消息。(虽然我确实收到编译错误消息。)

如果我这样做:

crepl -Istdio 'char * str="text"; char * copy; int i=0; while(str[i] != '\''\0'\''){copy[i]=str[i]; i++;} printf("%s\n", copy);'

我没有得到任何输出。但我真的应该得到这个:

Segmentation fault (core dumped)

因为我忘记了malloc。复制字符串的正确方法是这样的:

crepl -Istdio,string,stdlib 'char * str="text"; char * copy=malloc(strlen(str)); int i=0; while(str[i] != '\''\0'\''){copy[i]=str[i]; i++;} printf("%s\n", copy);'

这很烦人。如果我没有收到诸如 Segmentation Fault 之类的消息,我将很难学习 C。

我试过更换

system $compiled_code;

通过

system "$compiled_code 2>&1";

print `$compiled_code 2>&1`;

但两者都不起作用。

您知道如何修改我的脚本来获取这些错误消息吗?

谢谢。

最佳答案

您有时在终端中看到的Segmentation fault (core dumped) 消息不是由您启动的进程产生的,而是由启动该进程的shell 产生的。 p>

当它启动一个进程时,shell 会使用类似于 man 3 waitpid 的系统调用等待它。这样的系统调用会告诉进程是成功退出(使用 return_exit())还是被信号杀死。在最后一种情况下,shell 会显示一条特定于导致提前终止的信号的消息 (man 3 strsignal)。

在您的特定情况下,这不是启动您用 C 编写的进程的 shell,而是 perl 解释器。你的进程被杀死不会使 perl 也被杀死,所以你的 shell 不会显示这样的消息。

我不会写 perl,但我确定您可以将 system $compiled_code; 替换为与 fork()/exec()/waitpid()/strsignal 等效的东西()

使用 this page 的结尾,我认为您可以在脚本末尾尝试这样做。

system $compiled_code;
my $status = $?;
if ($status == -1) {
print "failed to execute: $!\n";
}
elsif ($status & 127) {
printf "child died with signal %d, %s coredump\n",
($status & 127), ($status & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $status >> 8;
}

关于无法使用 perl 中的系统命令获取已编译 C 程序执行的标准错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66035346/

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