简体   繁体   English

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

[英]How to make gcc uses march=native as default?

Is there a way to change the specs file so that it will pass -march=native if nothing is specified in command line? 如果在命令行中未指定任何内容,是否可以更改specs文件,使其通过-march=native

Related things in the default specs file is: 默认规范文件中的相关内容是:

*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)}

I am not sure how specs works. 我不确定规格如何工作。 Simply specifying -march=native before or after %(cc1_cpu) doesn't work. 仅在%(cc1_cpu)之前或之后指定-march=native无效。 However, this line does take effect because GCC will report error if I put -something_wierd instead of -march=native . 但是,此行确实会生效,因为如果我放-something_wierd而不是-march=native那么GCC会报告错误。

Another thing I noticed is if I put %{march=i386:-something_wierd} before %(cc1_cpu) , gcc reports error so looks like -march=i386 is always passed in if nothing is specified, so is there a way to distinguish between nothing specified and -march=i386 in specs file? 我注意到的另一件事是,如果我将%{march=i386:-something_wierd} %(cc1_cpu) %{march=i386:-something_wierd}放在%{march=i386:-something_wierd} %(cc1_cpu)之前,则gcc报告错误,因此,如果未指定任何内容,则似乎总是传递-march=i386 ,因此是否有办法区分未指定任何内容,并且在规格文件中未指定-march=i386

BTW, what does %> do? 顺便说一句, %>做什么的? Seems like it is not specified in the documentation . 似乎未在文档中指定。

I am using MinGW's gcc-4.6.2 . 我正在使用MinGW的gcc-4.6.2

Referring to your last question: The gcc 4.6.1 sources ( gcc/gcc.c ) contain the following comment on %> : 关于最后一个问题: gcc 4.6.1源( gcc/gcc.c )对%>包含以下注释:

 %>S    Similar to "%<S", but keep it in the GCC command line.

For the sake of completeness following the comment for %< form the same file: 为了完整起见,请在%<的注释后面加上同一文件:

 %<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.

To answer the first question in short: yes, but .... 简短地回答第一个问题:是的,但是..

... the only generic solution I found has the significant drawback that the -march option will be ignored, so every build is done as if -march=native had been specified. ...我发现的唯一通用解决方案具有明显的缺点,即-march选项将被忽略,因此每次构建都像指定了-march=native一样进行。 Anyhow there is a workaround to that. 无论如何,有一种解决方法。

1 The solution (without workaround) 1解决方案(没有解决方法)

Create a specs-file called let's say specs.nativealways containing: 创建一个名为specs.nativealways的specs文件,该文件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)}

When using the specs-file (for example by invoking gcc with the option -specs=specs.nativealways ) the build will be done as if -march=native was specified (with the mentioned drawback that any occurrence of option -march=<arch> would have simply been ignored). 使用specs文件时(例如,通过使用带有-specs=specs.nativealways的选项调用gcc ),将像指定-march=native一样完成构建(具有提到的缺点,即-march=<arch>任何出现-march=<arch>会被忽略)。

2 The workaround 2解决方法

To still by able to override the newly configured default behavior one can use a modified version of the specs-file described above, introducing a new option called -myarch using the same syntax as -march (except for -myarch=native , which won't work, which does not metter as native now is the default). 为了仍然能够覆盖新配置的默认行为,可以使用上述specs文件的修改版本,引入一个名为-myarch的新选项, -myarch使用与-march相同的语法( -myarch=native除外,该选项-myarch正常工作,默认情况下不支持native )。

The modfied specs-file looks like this: 修改后的specs文件如下所示:

*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)}

PS: This has been tested with with gcc 4.6.2 on Linux, but should work on MinGW. PS:已在Linux上使用gcc 4.6.2进行了测试,但应在MinGW上运行。

*cc1_cpu:
+ %{!march*:-march=native}

While not a direct answer to your question, you can reach a very similar effect by defining CFLAGS and CXXFLAGS in your shell's initialization file. 虽然不是您问题的直接答案,但是通过在Shell的初始化文件中定义CFLAGSCXXFLAGS可以达到非常相似的效果。 99% of the Makefiles are sufficiently standard to pick up the environment values and pass the flags to gcc . 99%的Makefile是足够标准的,可以拾取环境值并将标志传递给gcc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM