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

Whi*_*ist 3 c unix linux perl gcc

我写了一个对我非常有用的漂亮的小 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
Run Code Online (Sandbox Code Playgroud)

这是脚本:

#!/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;
Run Code Online (Sandbox Code Playgroud)

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

如果我做这样的事情:

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

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

分段错误(核心转储)

因为我忘记了 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);'
Run Code Online (Sandbox Code Playgroud)

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

我试过更换

系统 $compiled_code;

经过

系统“$compiled_code 2>&1”;

或者

打印`$compiled_code 2>&1`;

但两者都不起作用。

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

谢谢。

pro*_*-fh 5

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

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

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

我不会写 perl,但我确信你可以用system $compiled_code;相当于fork()/exec()/waitpid()/strsignal().

使用结束此页,我觉得你可以在你的脚本的最后尝试。

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;
}
Run Code Online (Sandbox Code Playgroud)