198

I'm compiling my C++ app using GCC 4.3. Instead of manually selecting the optimization flags I'm using -march=native, which in theory should add all optimization flags applicable to the hardware I'm compiling on. But how can I check which flags is it actually using?

vartec
  • 131,205
  • 36
  • 218
  • 244

5 Answers5

179

You can use the -Q --help=target options:

gcc -march=native -Q --help=target ...

The -v option may also be of use.

You can see the documentation on the --help option here.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 14
    I'm going to suggest that this is suboptimal. The output of --help=target doesn't display CPU cache information, of which the methods both elias and 42n4 below have listed. Specifically, on gcc 4.9.2 on a Phenom, the output includes these: ``--param l1-cache-size=64 --param l1-cache-line-size=64 --param l2-cache-size=512`` – Daniel Santos Jan 29 '15 at 00:22
  • @DanielSantos: on my system it does display those parameters with the `-v` option, albeit as part of the `cc1` command line... – thkala Jan 30 '15 at 10:20
  • not perfect. on gcc version 5.4.0 (Buildroot 2017.05-rc2-00016-gc7eaf50-dirty) it will cause the error in return: Assembler messages: Error: unknown architecture native Error: unrecognized option -march=native. So, loose the `-march=native` and it will work everywhere just following: `gcc -Q --help=target`. – Oleg Kokorin Jun 06 '17 at 14:00
  • @Oleg - That sounds like a bug in GCC 5. The issue is not present in GCC 7. – jww Oct 27 '17 at 19:50
154

To see command-line flags, use:

gcc -march=native -E -v - </dev/null 2>&1 | grep cc1

If you want to see the compiler/precompiler defines set by certain parameters, do this:

echo | gcc -dM -E - -march=native
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
elias
  • 1,565
  • 1
  • 9
  • 2
29

It should be (-### is similar to -v):

echo | gcc -### -E - -march=native 

To show the "real" native flags for gcc.

You can make them appear more "clearly" with a command:

gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )//g'

and you can get rid of flags with -mno-* with:

gcc -### -E - -march=native 2>&1 | sed -r '/cc1/!d;s/(")|(^.* - )|( -mno-[^\ ]+)//g'
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
42n4
  • 1,292
  • 22
  • 26
14

If you want to find out how to set-up a non-native cross compile, I found this useful:

On the target machine,

% gcc -march=native -Q --help=target | grep march
-march=                               core-avx-i

Then use this on the build machine:

% gcc -march=core-avx-i ...
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • This will not include all the flags unfortunately. – Baptiste Wicht Oct 16 '14 at 07:41
  • @BaptisteWicht are there flags that -march=native will include that -march=core-avx-i would not, in this case, or which flags? Thanks! – rogerdpack Jun 28 '16 at 20:08
  • 3
    @rogerdpack On this computer (sandybridge), march=sandybridge does not enable AVX (don't know why) while march=native does. Another important difference is that cache sizes are only extracted with march=native – Baptiste Wicht Jun 29 '16 at 07:03
  • 1
    @BaptisteWicht that's odd seems to work here (I guess): `echo | gcc-6 -dM -E - -march=sandybridge | grep AVX #define __AVX__ 1` but cache sizes do seem absent. – rogerdpack Jul 14 '16 at 00:37
8

I'm going to throw my two cents into this question and suggest a slightly more verbose extension of elias's answer. As of gcc 4.6, running of gcc -march=native -v -E - < /dev/null emits an increasing amount of spam in the form of superfluous -mno-* flags. The following will strip these:

gcc -march=native -v -E - < /dev/null 2>&1 | grep cc1 | perl -pe 's/ -mno-\S+//g; s/^.* - //g;'

However, I have only verified the correctness of this on two different CPUs (an Intel Core2 and AMD Phenom), so I suggest also running the following script to be sure that all of these -mno-* flags can be safely stripped.

2021 EDIT: There are indeed machines where -march=native uses a particular -march value, but must disable some implied ISAs (Instruction Set Architecture) with -mno-*.

#!/bin/bash

gcc_cmd="gcc"

# Optionally supply path to gcc as first argument
if (($#)); then
    gcc_cmd="$1"
fi

with_mno=$(
    "${gcc_cmd}" -march=native -mtune=native -v -E - < /dev/null 2>&1 |
    grep cc1 |
    perl -pe 's/^.* - //g;'
)
without_mno=$(echo "${with_mno}" | perl -pe 's/ -mno-\S+//g;')

"${gcc_cmd}" ${with_mno}    -dM -E - < /dev/null > /tmp/gcctest.a.$$
"${gcc_cmd}" ${without_mno} -dM -E - < /dev/null > /tmp/gcctest.b.$$

if diff -u /tmp/gcctest.{a,b}.$$; then
    echo "Safe to strip -mno-* options."
else
    echo
    echo "WARNING! Some -mno-* options are needed!"
    exit 1
fi

rm /tmp/gcctest.{a,b}.$$

I haven't found a difference between gcc -march=native -v -E - < /dev/null and gcc -march=native -### -E - < /dev/null other than some parameters being quoted -- and parameters that contain no special characters, so I'm not sure under what circumstances this makes any real difference.

Finally, note that --march=native was introduced in gcc 4.2, prior to which it is just an unrecognized argument.

Daniel Santos
  • 3,098
  • 26
  • 25