如何让gcc使用march = native作为默认值?

Kan*_* Li 5 gcc gcc4

有没有办法更改specs文件,以便-march=native在命令行中没有指定任何内容时它将通过?

默认规范文件中的相关内容是:

*cc1:
%(cc1_cpu)

*cc1_cpu:
%{march=native:%>march=native %:local_cpu_detect(arch)   %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
Run Code Online (Sandbox Code Playgroud)

我不确定规格如何运作.简单地指定-march=native之前或之后%(cc1_cpu)不起作用.不过,此行确实需要的效果,因为如果我把GCC会报告错误-something_wierd,而不是-march=native.

我注意到的另一件事是,如果我%{march=i386:-something_wierd}之前%(cc1_cpu),gcc报告错误,所以看起来-march=i386总是传入,如果没有指定,所以有没有办法区分指定的任何内容和-march=i386specs文件?

顺便说一下,该怎么%>办?好像它没有在文档中指定.

我正在使用MinGW gcc-4.6.2.

alk*_*alk 4

参考您的最后一个问题: gcc 4.6.1 来源 ( gcc/gcc.c) 包含以下评论%>

 %>S    Similar to "%<S", but keep it in the GCC command line.
Run Code Online (Sandbox Code Playgroud)

为了完整起见,请遵循对%<同一文件的注释:

 %<S    remove all occurrences of -S from the command line.
        Note - this command is position dependent.  % commands in the
        spec string before this one will see -S, % commands in the
        spec string after this one will not.
Run Code Online (Sandbox Code Playgroud)

简而言之回答第一个问题:是的,但是......

...我发现的唯一通用解决方案有一个显着的缺点:-march选项将被忽略,因此每个构建都会像-march=native已指定一样完成。无论如何,有一个解决方法。

1 解决方案(无解决方法)

创建一个名为的规格文件,specs.nativealways假设包含:

*cc1_cpu:
%<march=* -march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
Run Code Online (Sandbox Code Playgroud)

当使用specs文件时(例如通过使用gcc选项调用-specs=specs.nativealways),构建将像-march=native指定的一样完成(提到的缺点是任何出现的选项-march=<arch>都会被忽略)。

2 解决方法

为了仍然能够覆盖新配置的默认行为,我们可以使用上述规范文件的修改版本,引入一个名为的新选项,-myarch使用与以下相同的语法-march(除了-myarch=native,它不起作用,它不符合native现在是默认值)。

修改后的规格文件如下所示:

*cc1_cpu:
%<march=* %{myarch=*:%<myarch* -march=%* ; :-march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}}  %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
Run Code Online (Sandbox Code Playgroud)

PS:这已经在 Linux 上使用 gcc 4.6.2 进行了测试,但应该可以在 MinGW 上运行。