A. K.
A. K.

Reputation: 38270

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 error, 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?

Upvotes: 1

Views: 146

Answers (2)

dumb
dumb

Reputation: 86

IPC::System::Simple/IPC::Run3 make this easier

Upvotes: 1

cnicutar
cnicutar

Reputation: 182774

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? */
}

Upvotes: 6

Related Questions