首页 > 解决方案 > Perl Inline::C default flags

问题描述

I made a module using Inline::C and I noticed some unexpected performance discrepancies between running it on the host MacOS vs a guest Linux VM. Looking into it, it was due to the default C compiler flags being different. On MacOS they appear to be:

-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -O3   -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\"

Vs on Centos 7:

 -fPIC -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2 -O2   -DVERSION=\"0.00\" -DXS_VERSION=\"0.00\"

The main difference for my code is O3 vs O2, so I looked into the Inline docs and used:

use Inline (C => Config => ccflags => '-O3');

To explicitly specify -O3. Well, the result is that -O3 -O2 is applied that way, so specifying ccflags does not overwrite the default, it just adds before them, which in the end does not have any effect. Any idea where the default comes from and/or how to overwrite it to specify the optimization level that I want.

标签: perlinline-c

解决方案


似乎添加optimize配置选项可以满足您的需求。这是一个非常简短的示例,其中包含添加之前optimize => '-O3'和之后的输出:

use warnings;
use strict;

use Inline 'C';

use Inline C => 'Config',
    build_noisy => 1,
    force_build => 1,
    optimize => '-O3',
;

print add(5, 6);

__END__
__C__

int add (int x, int y){
    return(x + y);
}

这是输出(为简洁起见):

前:

cc -c -I"/home/steve/scratch/inline" -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2

后:

cc -c -I"/home/steve/scratch/inline" -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O3

...在 Linux Mint 18.3 上。

默认值来自,它在系统上编译/构建$Config{optimize}时存储为只读默认值。perl


推荐阅读