bliako has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I wonder if what I see is a bug or it is a mistake on my part:

The following script will attempt to compile and link a sample C file (warning: it writes what's in __DATA__ as a file on your current dir) using ExtUtils::CBuilder. However, the compile() sub ignores the optimize flags I try to set. Basically I want to remove -g.

#!/usr/bin/env perl use strict; use warnings; use ExtUtils::CBuilder; my $cfile = 'hello.c'; (my $objfile = $cfile) =~ s/\.c$/.o/; # warning, it writes C program to your disk open(my $FH, '>', $cfile) or die "could not write C file ($cfile) to d +isk, $!"; print $FH $_ for(<DATA>); close($FH); die "failed to write C file ($cfile) to disk." unless -f $cfile; # warning it will produce object file on your disk my $cbuilder = ExtUtils::CBuilder->new( config => { optimize => '-O7', }, ); $cbuilder->compile( source => $cfile, object_file => $objfile ); __DATA__ #include <stdio.h> int main(void){ printf("hello\n"); }

What I get (in linux, perl v 5.26.2, ExtUtils::CBuilder 0.280230) is compile() uses its own optimization (-O2 -g) and also mine (-O7):

gcc -I/usr/lib64/perl5/CORE -fPIC -c -D_REENTRANT -D_GNU_SOURCE -O2 -g + -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexcept +ions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc- +switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=g +eneric -fasynchronous-unwind-tables -fwrapv -fno-strict-aliasing -I/u +sr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O7 -o he +llo.o hello.c

bw, bliako

Replies are listed 'Best First'.
Re: ExtUtils::CBuilder->compile() ignores flags
by choroba (Cardinal) on Jan 04, 2019 at 18:41 UTC
    The config from the constructor should override the Config value, if I read the code correctly.

    Maybe the flags are coming from a different option? Examine $Config{ccflags}, or existence of $ENV{CFLAGS} etc. Maybe add debugging prints to the source of Base.pm to show what values it processes and how?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I have emptied CFLAGS (edit:and CCFLAGS, and from the prompt): $ENV{CFLAGS}=''; but no change...

      So, I did this:

      # before compile() is called (and after new()) $cbuilder->{config}->{ccflags} =~ s/(?<=\s)\-O[0-9]{0,2}(?=\s|$)//g; # + remove -OXX $cbuilder->{config}->{ccflags} =~ s/(?<=\s)\-g(?=\s|$)//g # remove -g

      Thanks