how to detect when compiler emits an error
To compile a C++
project, I want to write a perl
script to compile my program and see if the compilation went wrong or not. If the compiler gives any compilation erro开发者_C百科r, I'll need to perform some other task.
The perl
script will be something like this:
@l1 = `find . -name '*.c'`;
@l2 = `find . -name '*.cpp'`;
@l3 = `find . -name '*.cc'`;
my $err;
my $FLAGS = "-DNDEBUG"
push(@l , @l1, @l2, @l3);
chomp(@l);
foreach (@l) {
print "processing file $_ ...";
$err = `g++ $_ $FLAGS`;
if($err == something) {
#do the needful
}
}
so what should be something?
You should check $?
instead, after g++...
.
perlvar
$?
The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator.
The exit value of the subprocess is really
($?>> 8)
So you should check if g++
returned 0
(success) or non-zero
.
if ($? >> 8) {
/* Error? */
}
IPC::System::Simple/IPC::Run3 make this easier
精彩评论